47.Number of Islands(岛的数量)
Level:
Medium
题目描述:
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:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
思路分析:
寻找岛屿的数量,如果遇到一个1,四周都是0,那么为一个岛,只要水平或者垂直临近的1都算是1个岛,求一个二维数组中岛的个数。即求数组中不相邻的1的个数,解决思路,遍历数组,碰到一个1,就把周围所有相连的1都标记为非1,这样整个遍历过程中碰到的1的个数就是所求的解。
代码:
public class Solution{
public int numIslands(char [][]grid){
if(grid==null||grid.length==0)
return 0;
int res=0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]!='1')
continue;
res++;
dfs(grid,i,j);
}
}
return res;
}
public void dfs(char [][]grid,int i,int j){
if(i<0||i==grid.length||j<0||j==grid[0].length)
return;
if(grid[i][j]=='1'){
grid[i][j]='0';
dfs(grid,i+1,j);
dfs(grid,i,j+1);
dfs(grid,i-1,j);
dfs(grid,i,j-1);
}
}
}
47.Number of Islands(岛的数量)的更多相关文章
- LeetCode Number of Islands 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- [LeetCode] Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- LeetCode 200:岛屿数量 Number of Islands
题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- ES6笔记总结
常用命令 函数的rest参数和扩展 promise使用 module.exports和Es6 import/export的使用 function sum(x,y,z){ let total = 0; ...
- 算法学习--Day4
今天写了两章题目,仍然是比较基础的内容.感觉时间好紧张,怕来不及,所以以后要加快速度了. 今天写的最多的是查找类题目,关键是二分查找的掌握. 题目描述 输入一个数n,然后输入n个数值各不相同,再输入一 ...
- 51nod1414【思维】
思路: 直接可以枚举1-n,如果枚举到是n的约数i,那么暴力枚举起点,其余点用i累加就一定是正多边形.复杂度是(n*n的公约数个数(最多80)): const int N=2e4+10; int a[ ...
- 【Unity3D】3D角色换装++ Advance
http://www.cnblogs.com/dosomething/archive/2012/12/15/2818897.html 本文在之前的文章Unity3D角色换装的原理 基础上做一个补充 给 ...
- [HNOI2010] 合唱队 chorus
标签:区间DP.题解: 首先分析题目,根据题目中的列队方式以及数据范围,我们容易想到O(n2)的算法,也就是区间DP.发现直接dp[L][R],不能转移,于是添加一个dp[L][R][0/1],0表示 ...
- 百度搜索:有关Baiduspider的10个问题
猫宁!!! 参考链接: http://help.baidu.com/question?prod_id=99&class=476&id=2996 https://ziyuan.baidu ...
- IOS正则表达式 (身份证、电话、汉字等常用条件筛选)
下面的正则列表 替换对应的正则规则 那个字符串就可以了 例如: //正则规则 NSString *regex = @"^((13[0-9])|(147)|(17[0-9])|(15[^ ...
- B.小A与任务
链接:https://ac.nowcoder.com/acm/contest/369/B 题意: 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i ...
- Codeforces 1139D(推式子+dp)
题目传送 推公式博客传送 推完式子就是去朴素地求就行了Orz const int maxn = 1e5 + 5; const int mod = 1e9 + 7; int m, mu[maxn], v ...
- UVA11292(排序贪心)
笔者休息娱乐. ; int n, m, a; ll solve(int n, int m) { ll ans = ; priority_queue<int, vector<int>, ...