Advertisement
courtneythurston

Untitled

Mar 8th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. class Solution {
  2.     public int numIslandsForReal(char[][] grid, int row, int col, int numberOfRows, int numberOfCols, int isIsland) {
  3.         if ((grid[row][col] == '0') || (grid[row][col] == '9')){
  4.             return 0;
  5.         }
  6.         if (isValid(grid, row, col, numberOfCols, numberOfRows)){
  7.             if (grid[row][col] == '1'){
  8.                 grid[row][col] = '9';
  9.                 isIsland = numIslandsForReal(grid, row+1, col+1, numberOfRows, numberOfCols, isIsland);
  10.             }
  11.         }
  12.         isIsland++;
  13.         return 1;
  14.     }
  15.    
  16.     public boolean isValid(char[][] grid, int row, int col, int numberOfCols, int numberOfRows){
  17.         if ((row < numberOfRows) && (col < numberOfCols) && (row >= 0) && (col >= 0)){
  18.             return true;
  19.         }
  20.         else{
  21.             return false;
  22.         }
  23.     }
  24.    
  25.     public int numIslands(char[][] grid)
  26.     {
  27.         int row = 0;
  28.         int col = 0;
  29.         int numberOfRows = grid.length;
  30.         int numberOfCols = grid[0].length;
  31.         int count = 0;
  32.         int isIsland = 0;
  33.         for (int i=0; i<numberOfRows; i++){
  34.             for (int j=0; i<numberOfCols; j++){
  35.                 count += numIslandsForReal(grid, row, col, numberOfRows, numberOfCols, isIsland);
  36.             }
  37.         }
  38.         return count;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement