Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int numIslandsForReal(char[][] grid, Stack<Integer> stack, int row, int col, int numberOfRows, int numberOfCols, int count, int tempRow, int tempCol) {
- if ((grid[row][col] == '0') || (grid[row][col] == '9')){
- return count;
- }
- if (isValid(grid, row, col, numberOfCols, numberOfRows)){
- tempRow = stack.pop();
- tempCol = stack.pop();
- if (grid[tempRow][tempCol] == '1'){
- grid[tempRow][tempCol] = '9';
- stack.push(tempRow+1);
- stack.push(tempCol+1);
- count = numIslandsForReal(grid, stack, row+1, col+1, numberOfRows, numberOfCols, count, tempRow, tempCol);
- }
- }
- count++;
- return count;
- }
- 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)
- {
- Stack<Integer> stack = new Stack<Integer>();
- int row = 0;
- int col = 0;
- int numberOfRows = grid.length;
- int numberOfCols = grid[0].length;
- int tempRow = 0;
- int tempCol = 0;
- int count = 0;
- stack.push(0); //row
- stack.push(0); //col
- return numIslandsForReal(grid, stack, row, col, numberOfRows, numberOfCols, count, tempRow, tempCol);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement