Number of Islands
来源:互联网 发布:电脑软件安装后打不开 编辑:程序博客网 时间:2024/05/16 01:08
public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } 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++; helper(grid, i, j); } } } return count; } private void helper(char[][] grid, int i, int j) { grid[i][j] = 0; if (i - 1 >= 0 && grid[i - 1][j] == '1') { helper(grid, i - 1, j); } if (i + 1 < grid.length && grid[i + 1][j] == '1') { helper(grid, i + 1, j); } if (j - 1 >= 0 && grid[i][j - 1] == '1') { helper(grid, i, j - 1); } if (j + 1 < grid[0].length && grid[i][j + 1] == '1') { helper(grid, i, j + 1); } }}
0 0
- Number of Islands
- Number of Islands
- Number of Islands
- [LeetCode] Number of Islands
- Leetcode-Number of Islands
- Number of Islands
- Leetcode Number of Islands
- [LeetCode]Number of Islands
- Number of Islands
- Number of Islands
- [leetcode]Number of Islands
- [leetcode] Number of Islands
- leetcode Number of Islands
- leetcode:Number of Islands
- [leetcode] Number of Islands
- 200 Number of Islands
- [LeetCode] Number of Islands
- Number of Islands
- 个人主页
- Python: 函数式编程
- Merge k Sorted Lists
- Word Break
- Integer to English Words
- Number of Islands
- Python: 文件操作
- Integer最大值2147483647
- Dynamically size the height of the table view cells
- Python: 异常处理
- 72. Edit Distance
- 个人主页
- [转】 spyder-python在windows下的安装
- 勾股定理一日一证连载77