【刷题-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 ...
随机推荐
- grep的时候Binary file matches **.log 怎么解决
操作 grep "hello world" test.log 结果 Binary file test.log matches 原因:grep认为test.log是二进制文件 解决方 ...
- python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()
import time 获取当前时间: 指定字符串格式:time.strftime("%Y-%m-%d %H:%M:%S") 当前时间戳:time.time() 当前时间元组格式 ...
- vim 默认配置
vim ~/.vimrc 然后输入常用的需要用的命令,然后保存,这个每次启动VIM都会自动配置.当然你也可以在VIM里面按":"之后输入如下命令,但是下次启动之后设置就会丢失了. ...
- SpringBoot内嵌ftp服务
引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.ftpserver/ftpserver-core --> <depen ...
- 使用JS对字符串进行MD5加密
md5.js /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorith ...
- 优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download
优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download TTFB全称Time To First Byte,是指网络请求被发起到从服务器接收到地 ...
- 一个windows主题网站-
地址 地址 说明 选择适合自己的,下载,双击安装,就可使用了
- Qt5使用QSqlQuery读写sqlite3数据库
概述 本文将介绍使用 Qt5使用QSqlQuery读写sqlite3. 设计初衷: 项目需要使用配置文件,配置文件使用的是sqlite3 , 这是V1.0.0, 后期增加其他功能. 需要C++11支持 ...
- 防止 jar 包被反编译
1.隔离Java程序 最简单的方法就是让用户不能够访问到Java Class程序,这种方法是最根本的方法,具体实现有多种方式.例如,开发人员可以将关键的Java Class放在服务器端,客户端通过访问 ...
- CS5211|DP转LVDS |低成本DP to LVDS Conversion 方案设计
目前市面上DP转LVDS转换--DP to LVDS Conversion 方案设计有以下: 龙迅LT8911 LT7211.普瑞PS8625.昆泰CH7511等方案,DP换LVDS转换主要是用在一些 ...