Leetcode 200. Number of Islands
2016-01-21 04:41
531 查看
Leetcode 200. Number of Islands
Given a 2d grid map of
(water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Answer: 1
Example 2:
Answer: 3
Analysis:
This is quite similar to the problem I have met before in the computer vision course that to count the number of the objects in a picture.
And For this problem We can use the DFS and BFS method. DFS is quite simple. just to write the recursive method to loop for the whole value, and a important method to use is to use a two dimension array to reduce the number of implementation of the method.
Code
Another method is BFS, here is the method to use BFS, http://blog.csdn.net/guorudi/article/details/44997949
Use queue to store the pair which is a class for the x and y position value, and use BFS to recursivlly visit every element.
Given a 2d grid map of
'1's (land) and
'0's
(water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110 11010 11000 00000
Answer: 1
Example 2:
11000 11000 00100 00011
Answer: 3
Analysis:
This is quite similar to the problem I have met before in the computer vision course that to count the number of the objects in a picture.
And For this problem We can use the DFS and BFS method. DFS is quite simple. just to write the recursive method to loop for the whole value, and a important method to use is to use a two dimension array to reduce the number of implementation of the method.
Code
public class Solution { public int numIslands(char[][] grid) { int rowNum = grid.length; if(rowNum == 0) return 0; int colNum = grid[0].length; if(colNum == 0) return 0; int [][] record = new int[rowNum][colNum]; int number = 0; for(int i = 0; i < rowNum; i++){ for(int j = 0; j < colNum ; j++){ if(grid[i][j] != '1') record[i][j] = 0; if(grid[i][j] == '1' && record[i][j] == -1){ num++; dfsdfs(grid, record, number, row, col); } } } return number; } public void dfs(char[][]grid, int[][]record, int number, int row, int col) { int rowNum = grid.length; int colNum = grid[0].length; record[row][col] = number; // if(row < 0 || row >= rowNum) // return; // // if(col < 0 || row >= colNum) // return; // // if(grid[row][col] != '1') // record[row][col] = 0; // else if(record[i][j] != -1) // return; if(row - 1 >= 0 && grid[row - 1][col] == '1' || record[row - 1][col] == -1) dfs(grid, record, number, row - 1, col); if(row + 1 < rowNum && grid[row + 1][col] == '1' || record[row + 1][col] == -1) dfs(grid, record, number, row + 1, col); if(col - 1 >= 0 && grid[row][col - 1] == '1' || record[row][col - 1] == -1) dfs(grid, record, number, row, col - 1); if(col + 1 < colNum && grid[row][col + 1] == '1' || record[row][col + 1] == -1) dfs(grid, record, number, row, col + 1); } }
Another method is BFS, here is the method to use BFS, http://blog.csdn.net/guorudi/article/details/44997949
Use queue to store the pair which is a class for the x and y position value, and use BFS to recursivlly visit every element.
相关文章推荐
- 对结合BDD进行DDD开发的一点思考和整理
- LNMP环境搭建
- cf#gb2015-C. New Year and Domino-dp
- cf#gb2015-B - New Year and Old Property-暴力
- Leetcode 328. Odd Even Linked List
- 阿里云 CentOS下面 Mysql 数据迁移
- 第六章 学习笔记
- 大型数据库分析-关联式规则(Associaton-Rules)-1
- 自学Java Swing日志(一)
- 须佐能乎
- 机器学习-隐马尔可夫模型(Hidden Markov Modelle)
- 机器学习-贝叶斯学习(BayesLernen)-II
- 最大连续子序列问题的java实现
- WCF学习之三, 寄宿方式 代码,配置文件
- 多线程技术防止cell图片重复下载
- Leetcode 329. Longest Increasing Path in a Matrix
- C# delegate
- Java编码规范
- 小娜学法(9):什么是包庇?
- 微赞,你正在使用本地安装, 但未下载完整安装包, 请从微赞官网下载完整安装包后重试.