Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int numIslandsForReal(char[][] grid, int row, int col, int numberOfRows, int numberOfCols, int isIsland) {
- if ((grid[row][col] == '0') || (grid[row][col] == '9')){
- return 0;
- }
- if (isValid(grid, row, col, numberOfCols, numberOfRows)){
- if (grid[row][col] == '1'){
- grid[row][col] = '9';
- isIsland = numIslandsForReal(grid, row+1, col+1, numberOfRows, numberOfCols, isIsland);
- }
- }
- isIsland++;
- return 1;
- }
- public boolean isValid(char[][] grid, int row, int col, int numberOfCols, int numberOfRows){
- if ((row < numberOfRows) && (col < numberOfCols) && (row >= 0) && (col >= 0)){
- return true;
- }
- else{
- return false;
- }
- }
- public int numIslands(char[][] grid)
- {
- int row = 0;
- int col = 0;
- int numberOfRows = grid.length;
- int numberOfCols = grid[0].length;
- int count = 0;
- int isIsland = 0;
- for (int i=0; i<numberOfRows; i++){
- for (int j=0; i<numberOfCols; j++){
- count += numIslandsForReal(grid, row, col, numberOfRows, numberOfCols, isIsland);
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement