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 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
这个题目的意思就是1表示陆地,0表示是水,问你陆地有多少块,其实就是求最大联通块的数目。
可以想到用深搜去解决它。对于每一块陆地1进行Dfs,Dfs过了以后就把它标记为0,这样全部走一遍就知道有多少个联通块了。
接下来就是代码实现了,这次的代码还是遇见了一点小问题,先贴出有问题的代码:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int islandNum=0;
int row=grid.size();
int column=grid[0].size();
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
{
if(grid[i][j]=='1')
{
Dfs(grid,i,j);
islandNum++;
}
}
return islandNum;
}
void Dfs(vector<vector<char>>& g,int r,int c)
{
if(g[r][c]=='0') return;
g[r][c]='0';
if(r<g.size()-1)
Dfs(g,r+1,c);
if(c<g[0].size()-1)
Dfs(g,r,c+1);
}
};
报的错误是:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int islandNum=;
if(grid.size()==) return ;
if(grid[].size()==) return ;
int row=grid.size();
int column=grid[].size();
for(int i=;i<row;i++)
for(int j=;j<column;j++)
{
if(grid[i][j]=='')
{
Dfs(grid,i,j);
islandNum++;
}
}
return islandNum;
}
void Dfs(vector<vector<char>>& g,int r,int c)
{
if(g[r][c]=='') return;
g[r][c]='';
if(r<g.size()-)
Dfs(g,r+,c);
if(c<g[].size()-)
Dfs(g,r,c+);
if(r>)
Dfs(g,r-,c);
if(c>)
Dfs(g,r,c-);
}
};
leetcode题解 200. Number of Islands(其实就是一个深搜)的更多相关文章
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】200. Number of Islands (2 solutions)
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the 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 s ...
- 【刷题-LeetCode】200 Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 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 用了第一种方式, ...
- Java for 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] 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 ...
随机推荐
- 教你如何在win7中的cygwin64下安装hadoop
首先我们要准备如下环境及软件: win7(64位) cygwin - jdk-6u25-windows-x64.zip hadoop-.tar.gz 1.在win7系统上正常安装jdk,同时注意设置好 ...
- GO格式化打印
General(通用占位符)  Integer整形  Integer width(指定长度的整型,以5为例)  Float(浮点数)  String(字符串)  String Width ( ...
- 软件工程---UML理解
1.依赖关系和关联关系 1.1依赖关系是调用关系,其又分为全局依赖.参数依赖.局部依赖 1.2关联关系是结构上的关系,按照关联的强弱又具体分为关联关系.聚合关系(整体和部分的组成关系.whole-pa ...
- 从路由器镜像中提取uImage头信息
uImage header为64字节,文件头为27 05 19 56 hexdump -C a.bin | grep "27 05 19 56" 或者 hd aa.bin | gr ...
- D3生成树专题
这一天不知道怎的上课 竟然我说了两道题正解: 第一题:我写过一篇较详细的博客:https://www.cnblogs.com/Tyouchie/p/10366967.html 第二题:UVA10369 ...
- Win10在右键菜单添加在此处打开命令窗口项
转载:https://jingyan.baidu.com/article/2d5afd6930107a85a2e28e2d.html 第一步:新建一个文本文档,输入如下代码,然后另存为OpenCmdH ...
- Bootstrap3基础 table-bordered/hover 表格加外边框和鼠标悬停对应行的背景色加深
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- log4net:ERROR ConfigureFromXml called with null 'element' parameter
场景重现 ASP.NET Core 下集成 log4net 时, 运行时报错如下: log4net:ERROR ConfigureFromXml called with null 'element' ...
- 关于ComponentOne For WinForm 的全新控件 – DataFilter数据切片器(Beta)
概述 数据切片器在电子商务网站上很常见 - 它们可以帮助用户快速过滤所选商品,并且所有过滤选项都可以在一个地方使用,通常包含核心控件类型为:清单,范围栏和单选按钮等.在ComponentOne For ...
- Lintcode423-Valid Parentheses-Easy
思路: 数据结构:stack.遍历整个字符串,如果遇到左向括号( [ { 则入栈.如果遇到右向括号时,先检查栈是否为空,为空说明左右向括号数目不一致,返回false:不为空则弹出栈顶元素查看是否和右向 ...