【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】
来源:互联网 发布:c语言程序的基本单位 编辑:程序博客网 时间:2024/05/16 09:10
【200-Number of Islands(岛的数目)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
代码下载【https://github.com/Wang-Jun-Chao】
原题
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:
11110110101100000000
Answer: 1
Example 2:
11000110000010000011
Answer: 3
题目大意
给定的一个二维网格的地图(’1’(陆地)和0(水)),计数岛的数量。岛屿是四面环水,是由相邻的陆地水平或垂直连接而形成的。你可以假设该网格的所有四个边都被水包围。
解题思路
一个网格,有一个相应的访问标记矩阵,对网格每个元素时行广度优先遍历(或者深度优先遍历),统计岛的数目
代码实现
算法实现类
public class Solution { public int numIslands(char[][] grid) { // 参数校验 if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } // 元素默认值是false boolean[][] visited = new boolean[grid.length][grid[0].length]; int result = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { // 如果此位置没有被访问过,并且此位置是岛,就里德广度优先遍历 if (!visited[i][j] && grid[i][j] == '1') { result++; bfs(grid, visited, i, j); } } } return result; } /** * 广度优先搜索 * @param grid 网格 * @param visited 访问标记矩阵 * @param row 横坐标 * @param col 纵坐标 */ private void bfs(char[][] grid, boolean[][] visited, int row, int col) { if (row >= 0 && row < grid.length // 行合法 && col >= 0 && col < grid[0].length // 列合法 && !visited[row][col] // 没有访问过 && grid[row][col] == '1') { // 是岛上陆地 // 标记此位置已经访问过了 visited[row][col] = true; // 上 bfs(grid, visited, row - 1, col); // 右 bfs(grid, visited, row, col + 1); // 下 bfs(grid, visited, row + 1, col); // 左 bfs(grid, visited, row, col - 1); } }}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47970795】
2 1
- 【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】
- [leetcode-200]Number of Islands(java)
- 【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】
- 【LeetCode-面试算法经典-Java实现】【172-Factorial Trailing Zeroes(阶乘尾后0的数目)】
- LeetCode(200) Number of Islands
- LeetCode 200 Number of Islands(DFS)
- Leetcode 200 Number of Islands 岛的个数
- 【LeetCode-面试算法经典-Java实现】【136-Single Number(只出现一次的数字)】
- [LeetCode 200]Number of Islands
- leetcode 200 : Number of Islands
- LeetCode #200 Number of Islands
- LeetCode(200)Number of Islands
- leetcode 200: Number of Islands
- [LeetCode 200] Number of Islands
- Leetcode #200 Number of Islands
- LeetCode 200 Number of Islands
- LeetCode 200 Number of Islands
- leetcode: Number of Islands(200)
- POJ 3645 解题报告
- 【LeetCode-面试算法经典-Java实现】【198-House Robber(抢劫犯)】
- Spring MVC MultiActionController example
- 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】
- Spring MVC MultiActionController annotation example
- 【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】
- Spring MVC PropertiesMethodNameResolver example
- 正则表达式测试工具 regexbuddy
- HDU 3625 Examining the Rooms(第一类斯特林数)
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- C语言的标准库概述
- Exactly-once Spark Streaming from Apache Kafka
- CentOS6.5 升级 Python 2.7 版本
- Leecode_Single Number