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 ...
随机推荐
- jQuery属性attr
设置多个属性/值对 为被选元素设置一个以上的属性和值. 语法 $(selector).attr({attribute:value, attribute:value ...})比如:$("im ...
- CSS 边框样式
CSS 边框样式 直线边框样式 <html> <body> <!-- border: 1px 边框像素为1.solid red 边框样式以及边框颜色 --> < ...
- HTTP的简单的解析
HTTP 中文全称为超文本传输协议是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议.它是一种通用的,不分状态(stateless)的协议,除了诸如名称服务和分布对象管理系统之类的超文本用途 ...
- Code Pages
https://docs.microsoft.com/en-us/windows/desktop/intl/code-pages Most applications written today han ...
- JavaScript的Let用法
let 语句声明一个块级作用域的本地变量,并且可选的将其初始化为一个值. 描述 let 允许你声明一个作用域或被限制在块级中的变量.语句或者表达式. 与var不同的是,它声明的变量只能是全局或者整个函 ...
- 误操作yum导致error: rpmdb
error: cannot open Packages index using db5 - (-30973) error: cannot open Packages database in /var ...
- 《Practical Vim》第八章:利用动作命令在文档中移动
面向单词的移动 定义: Vim 提供了面向单词的动作命令,用于将光标正向/反向移动一个单词; 功能 命令 正向移动到下一单词开头 w 反向移动到上一单词的开头 b 正向移动到下一单词(当前单词)的结尾 ...
- linux软件管理之概述
软件包管理 ====================================================================================安装/查询/卸载 一 ...
- HihoCoder - 1142 ,三分入门
先来说说三分的思想: 从三分法的名字中我们可以猜到,三分法是对于需要逼近的区间做三等分: 我们发现lm这个点比rm要低,那么我们要找的最小点一定在[left,rm]之间.如果最低点在[rm,right ...
- flask项目部署
1.安装 安装ssh 新版本安装位 apt/apt-get 老版本为 apt-get 安装以后 ifconfig查看当前的ip地址 如果网络不在同一个ip段 将网络模式设置为桥接 安装pip3 sud ...