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 ...
随机推荐
- GoldenGate实时投递数据到大数据平台(7)– Apache Hbase
Apache Hbase安装及运行 安装hbase1.4,确保在这之前hadoop是正常运行的.设置相应的环境变量, export HADOOP_HOME=/u01/hadoop export HBA ...
- python从字符串中提取指定的内容
有如下字符串: text=cssPath:"http://imgcache.qq.com/ptlogin/v4/style/32",sig:"OvL7F1OQEojtPk ...
- 2018.9.25 NOIP模拟赛
*注意:这套题目应版权方要求,不得公示题面. 从这里开始 Problem A XOR Problem B GCD Problem C SEG 表示十分怀疑出题人水平,C题数据和标程都是错的.有原题,差 ...
- https连接器
非对称性加密:A生成一份公私钥,将公钥交给需要进行数据传输的B,B发送数据时先用公钥对数据进行加密,然后发送给A,再由A使用私钥进行解密. 但存在漏洞即B如何确认公钥是由A提供的.因此需要一个第三方机 ...
- freeswitch 修改系统最大呼叫量
freeswitch 中有2个参数限制系统的最大呼叫量,以防止资源耗尽. max_session控制最大并发数.默认值1000:sps控制最大每秒呼叫量,默认值30 命令临时生效:fsctl max_ ...
- 第十届蓝桥杯2019年C/C++ 大学B组省赛试题
2019年第十届蓝桥杯大赛软件类省赛C/C++大学B组 试题 A:组队 本题总分:5分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1号位至 5号位各一名球员, 组成球队的首发阵容. 每位球 ...
- 【七】jquery之属性attr、 removeAttr、prop[全选全不选及反选]
全选全不选 界面: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 构建oracle12c的Docker镜像
1. 准备工作 需要下载oracle相关的东东,例如安装文件,dockerfile.这些都可以从oracle 的github 上面找到.https://github.com/oracle/docker ...
- springboot aop 不生效原因解决
最近参照资料创建Springboot AOP ,结果运行后aop死活不生效. 查明原因: 是我在创建AOP类时选择了Aspect类型,创建后才把这个文件改为Class类型,导致一直不生效, 代码配置这 ...
- hello2部分代码解析
/** * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.** You may not modify, us ...