【刷题-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. 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:
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
Example 2:
Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
解1 bfs
class Solution {
public:
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == 0)return 0;
vector<vector<bool>> vis(grid.size(), vector<bool>(grid[0].size(), false));
int ans = 0;
for(int i = 0; i < grid.size(); ++i){
for(int j = 0; j < grid[0].size(); ++j){
if(vis[i][j] == false && grid[i][j] == '1'){
ans++;
bfs(grid, vis, i, j);
//dfs(grid, vis, i, j);
}
}
}
return ans;
}
void bfs(vector<vector<char>>& grid, vector<vector<bool>>& vis,
int x, int y){
queue<pair<int, int>>q;
q.push(make_pair(x,y));
vis[x][y] = true;
while(!q.empty()){
int tmpx = q.front().first, tmpy = q.front().second;
q.pop();
for(int i = 0; i < 4; ++i){
int tmpxx = tmpx + dx[i], tmpyy = tmpy + dy[i];
if(valid(tmpxx, tmpyy, grid.size(), grid[0].size())
&& !vis[tmpxx][tmpyy] && grid[tmpxx][tmpyy]=='1'){
q.push(make_pair(tmpxx, tmpyy));
vis[tmpxx][tmpyy] = true;
}
}
}
}
};
解2 dfs
void dfs(vector<vector<char>>& grid, vector<vector<bool>>& vis,
int x, int y){
vis[x][y] = true;
for(int i = 0; i < 4; ++i){
int tmpx = x + dx[i], tmpy = y + dy[i];
if(valid(tmpx, tmpy, grid.size(), grid[0].size())
&& !vis[tmpx][tmpy] && grid[tmpx][tmpy] == '1'){
dfs(grid, vis, tmpx, tmpy);
}
}
}
【刷题-LeetCode】200 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 用了第一种方式, ...
- [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 ...
- 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 ...
- (BFS/DFS) 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 岛屿数量(C++/Java)
题目: 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 DFS
统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
随机推荐
- 使用.NET 6开发TodoList应用(5)——领域实体创建
需求 上一篇文章中我们完成了数据存储服务的接入,从这一篇开始将正式进入业务逻辑部分的开发. 首先要定义和解决的问题是,根据TodoList项目的需求,我们应该设计怎样的数据实体,如何去进行操作? 长文 ...
- 直接在filter过滤器代码里加org.apache.struts2.ServletActionContext.getRequest()会出现空指针情况
直接在filter过滤器代码里加org.apache.struts2.ServletActionContext.getRequest()获得request对象请注意啦,会出现空指针情况 请关注此处:
- 使用docker自定义oraclejdk启动jar包
Dockerfile文件 FROM centos:7 #把java与tomcat添加到容器中 ADD jdk-8u161-linux-x64.tar.gz /usr/local/ #安装 vim编辑器 ...
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- BeanUtils属性转换工具
commons 包的 BeanUtils 进行属性拷贝性能较差:Spring 的 BeanUtils 性能相对较好. public class A { private String name; pri ...
- matplotlib 高阶之Transformations Tutorial
目录 Data coordinates Axes coordinates Blended transformations 混合坐标系统 plotting in physical units 使用off ...
- vmware虚拟IOS系统
安装虚拟机 --以管理员的身份运行
- 使用.NET 6开发TodoList应用(12)——实现ActionFilter
系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 Filter在.NET Web API项目开发中也是很重要的一个概念,它运行在执行MVC响应的Pipeline中执行,允许我们 ...
- [opencv]<学习Opencv>英文原版翻译学习
[注]下文全部内容为 <<Learning OpenCV 3: Computer Vision in C++ with the OpenCV Library>>经由在线翻译整理 ...