200 Number of Islands 岛屿的个数
给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量。一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成。你可以假设网格的四个边均被水包围。
示例 1:
11110
11010
11000
00000
答案: 1
示例 2:
11000
11000
00100
00011
答案: 3
详见:https://leetcode.com/problems/number-of-islands/description/
Java实现:
class Solution {
public int numIslands(char[][] grid) {
int m=grid.length;
if(m==0||grid==null){
return 0;
}
int res=0;
int n=grid[0].length;
boolean[][] visited=new boolean[m][n];
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(grid[i][j]=='1'&&!visited[i][j]){
numIslandsDFS(grid,visited,i,j);
++res;
}
}
}
return res;
}
private void numIslandsDFS(char[][] grid,boolean[][] visited,int x,int y){
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != '1' || visited[x][y]){
return;
}
visited[x][y] = true;
numIslandsDFS(grid, visited, x - 1, y);
numIslandsDFS(grid, visited, x + 1, y);
numIslandsDFS(grid, visited, x, y - 1);
numIslandsDFS(grid, visited, x, y + 1);
}
}
C++实现:
class Solution {
public:
int numIslands(vector<vector<char> > &grid) {
if (grid.empty() || grid[0].empty())
{
return 0;
}
int m = grid.size(), n = grid[0].size(), res = 0;
vector<vector<bool> > visited(m, vector<bool>(n, false));
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (grid[i][j] == '1' && !visited[i][j])
{
numIslandsDFS(grid, visited, i, j);
++res;
}
}
}
return res;
}
void numIslandsDFS(vector<vector<char> > &grid, vector<vector<bool> > &visited, int x, int y) {
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] != '1' || visited[x][y])
{
return;
}
visited[x][y] = true;
numIslandsDFS(grid, visited, x - 1, y);
numIslandsDFS(grid, visited, x + 1, y);
numIslandsDFS(grid, visited, x, y - 1);
numIslandsDFS(grid, visited, x, y + 1);
}
};
200 Number of Islands 岛屿的个数的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] 0200. Number of Islands 岛屿的个数
题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...
- LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
随机推荐
- Node 即学即用 笔记 思维导图
Node即学即用 REPL(Read-Evaluate-Print-Loop) console.log .clear .help .exit require('http') ...
- Android 性能优化探究
使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...
- mongodb数据出现undefined如何查询
某些字段出现undefined,该如何查询? 如下: db.getCollection('license').find({"holder_code":{$type:"un ...
- Android开发pool解析xml
xml在开发中的作用不可小觑,很多时候我们都要用到这种文件,所以学习它的解析方式很是必要. 我们都知道java中xml的解析有:dom,SAX,但是Android下我们使用pool解析,是更为方便,而 ...
- 如何写好react组件
react 组件方面: 总结 React 组件的三种写法 及最佳实践 [涨经验] React组件编写思路(一) 使用react-router实现单页面应用时设置页面间过渡的两种方式 [翻译]基于 Cr ...
- IDE配置jvm参数
-------- IntelliJ IDEA 配置参数:-Xms34m -Xmx234m 内存初始化大小,最小和最大值: 测试代码: public class JVMDemoTest { public ...
- Oracle - 查询语句 - 分组函数
/* 分组函数 不能再select子句中出现普通的列,除非这个列在group by中给出 所有的空值都会被分为一组 分组过滤 SELECT FROM WHERE GROUPBY HAVING ORDE ...
- YTU 2901: G-险恶逃生II
2901: G-险恶逃生II 时间限制: 1 Sec 内存限制: 128 MB 提交: 44 解决: 14 题目描述 SOS!!!koha is trapped in the danger ...
- rsync+inotify 实现实时同步
inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件 inotify的特性需要linux内核2.6.13以上的支持 [root@test1 inotify-tools-3.13]# u ...
- artemplate include
include用于嵌入字模板 {{include 'template_name'}} 子模板 默认共享当前的数据 也可以自己指定数据 {{include 'template_name' templat ...