Algorithms—200.Number of Islands
来源:互联网 发布:禁止启动程序软件 编辑:程序博客网 时间:2024/05/17 03:37
思路,遍历二维char数组,判断其是否在map中,如果不在map中,且值为‘1’;那么将计数+1,并将点加入map中,然后递归查询周围4个点(考虑边界)情况将
public class Solution { public int numIslands(char[][] grid) {int il = 0;Map<String, String> map = new HashMap<String, String>();for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid[i].length; j++) {if (map.get("(" + i + "," + j + ")") == null) {if (grid[i][j] == '1') {il++;map = new Solution().ss(map, i, j, grid);}}}}return il; } public Map<String,String> ss(Map<String,String> map,int i,int j,char[][] grid){if (map.get("(" + i + "," + j + ")") == null && grid[i][j] == '1') {int LI = grid.length;int LJ = grid[i].length;map.put("(" + i + "," + j + ")", "1");if (i > 0) {// 上边检测map = ss(map, i - 1, j, grid);}if (i < LI - 1) {// 下边检测map = ss(map, i + 1, j, grid);}if (j > 0) {// 左边检测map = ss(map, i, j - 1, grid);}if (j < LJ - 1) {// 右边检测map = ss(map, i, j + 1, grid);}}return map;}}
1的点全部加入。
0 0
- Algorithms—200.Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 200. Number of Islands
- 用完成例程(Completion Routine)实现的重叠I/O模型
- Java Spring AOP的两种配置方式
- sql去重复
- Android通知栏实现APP下载更新(DownloadManager和自定义Notification2种方式实现)
- 使用Spring Boot快速构建应用
- Algorithms—200.Number of Islands
- overview of hevc
- ExecuteSQL Command for ArcMap
- web.xml中classpath是什么含义[转]
- 查找数据库的物理位置
- php mcrypt CBC
- 云计算—GFS文件系统架构分析
- 安装python MySQLdb报错:pymemcompat.h:10:20: 致命错误的解决
- Lmabda表达式读值