Skip to content

Latest commit

 

History

History
40 lines (36 loc) · 959 Bytes

README.md

File metadata and controls

40 lines (36 loc) · 959 Bytes

Number of Islands

Solution 1

/**
 * Question   : 200. Number of Islands
 * Complexity : Time: O(n^2) ; Space: O(1)
 * Topics     : DFS
 */
class Solution {
    private int[][] dirs = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

    public int numIslands(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    count++;
                }
                dfs(grid, i, j);
            }
        }
        return count;
    }

    private void dfs(char[][] grid, int row, int col) {
        if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) {
            return;
        }
        if (grid[row][col] == '0') {
            return;
        }
        grid[row][col] = '0';
        for (int[] dir : dirs) {
            dfs(grid, row + dir[0], col + dir[1]);
        }
    }
}