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 用了第一种方式, ...
随机推荐
- hdoj 1203 I NEED A OFFER! 【另类01背包】【概率背包】
题意:... 策略:动态规划. 由于是求至少能得到一个offer的概率,那我们能够反着求.求得不到一个offer的概率.最后用1减去就好了. 代码: #include<string.h> ...
- hdoj-1242-Rescue【广搜+优先队列】
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- TinyXml 与 Rapidxml效率对照
曾经在做开发中一直使用TinyXml,在网上搜索说Rapidxml的效率比tinyXml高.个人比較喜欢追求效率.所以忍不住尝试性使用Rapidxml. RapidXml 的官方站点例如以下: htt ...
- react-loadable 进行代码分割的基本使用
由上篇文章我们可以知道,我的项目,打包后的 js 是 7M 的,首屏就加载 7M 的文件虽然没什么特别大的缺点,但总不是最优的解决办法 因此我们用到了 react-router4 的 react-lo ...
- 嵌入式开发之davinci--- 8168 电源调试总结
http://www.61ic.com/Article/DaVinci/TMS320DM81x/201403/51863.html
- android中init.rc文件的解析问题
init.rc中文件里会通过import /init.${ro.hardware}.rc文件,这个ro.hardware应该是某个详细的属性.而这个ro.hardware赋值应该是在Init进程中赋值 ...
- 阐述Linux操作系统之rpm五种基本操作
Linux操作系统现在已经成为流行的操作系统,很多的人都开始学习,Linux操作系统包括了很多的专业知识,今天和大家讲讲Linux操作系统中的rpm基本操作.希望你学会本文中提到rpm的五种基本操作知 ...
- javaScript查找HTML元素
1.通过id查找 例:查找id="intro"元素 var x=document.getElementById("intro"); 2.通过标签名查找 例:查找 ...
- beego5---gosqlite安装
WindowsWindows下的安装也非常简单,只要到 SQLite3 的下载页面,下载 Windows 下的预编译包 DLL 的压缩包(sqlite-dll-win32-x86-XXX.zip),然 ...
- 求两个list的交集和并集
两个list的并集,只需去除重复元素即可: 将两个list放入同一个set中即可: 两个list的交集: 1将其中一个list放入set, 2循环另一个list,每次向set塞值, 3判断set的总数 ...