[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 的区域块块数。
属于 DFS 思想。
将所有 1 塞进一个容器中,从容器中取出一个 1 ,并将相邻走完的 1 也从容器中取出,视为一次取数。重复此操作直至容器中没有元素 1 ,则取出次数就是 1 的区域块块数。
unordered_map<string, pair<int, int>> val_pos;
vector<vector<char>> theGrid; /**
* remove all land connecting with [i, k] adjecent horizontally or vertically. and the pos.
*/
void removeLand(int i, int k){ theGrid[i][k] = ''; string str = to_string(i) + "_" + to_string(k);
val_pos.erase(str); if (i- >= && theGrid[i-][k] == ''){
removeLand(i-, k);
} if (k+ < theGrid[].size() && theGrid[i][k+] == ''){
removeLand(i, k+);
} if (i+ < theGrid.size() && theGrid[i+][k] == ''){
removeLand(i+, k);
} if (k- >= && theGrid[i][k-] == ''){
removeLand(i, k-);
}
} int numIslands(vector<vector<char>>& grid) { if (grid.size() == ){
return ;
} theGrid = grid;
for ( int i = ; i < grid.size(); i++){
for(int k = ; k < grid[].size(); k++){
if (grid[i][k] == ''){
string str = to_string(i) + "_" + to_string(k);
val_pos[str] = {i, k};
}
}
} int res = ; while(val_pos.size() > ){
int i = val_pos.begin()->second.first;
int k = val_pos.begin()->second.second;
removeLand(i, k);
res++;
} return res;
}
[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 ...
- 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 ...
- (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 岛屿数量(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 ...
随机推荐
- POJ 2044 Weather Forecast
意甲冠军:有一2*2云,而一个4*4范围.在当天密布区必须有雨.有云4招式种类 .期间希望不要下雨,并且一个地方不能有连续7天没下雨. 思路:首先解决一个地方不能有连续7天没下雨的情况,要让地图上的全 ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- c#中WebBrowser控件的使用方法
首先先来简单介绍一下webbrowser控件,这个控件是可以实现在form窗体中添加网页内容的.如图,我在form中加入了百度api,(百度地图api调用博客里有讲) 使用这个控件其实很简单 (1)第 ...
- jquery实现图片切换和js实现图片切换
jquery实现图片切换: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- JS和利用openssl的object C加密得到相同的aes加密密文
这是之前接到的一个工作内容,项目原本的登录操作是获得账号和密码以后,对密码进行一遍MD5加密,然后传递账号和密文到cgi文件.在c中获取到账户以后,从数据库中获取到密码,对密码进行一次MD5的加密,然 ...
- ListActivity ListView 使用 介绍 用法
ListActivity简单的说就是ListView和Activity的结合,跟ListView和Activity组合实现的没有什么很大的差别,主要是比较方便. 在实现时,要注意: 1.一般情况,Li ...
- 详解ASP.NET MVC应用程序请求生命周期
------转载当一个ASP.NET MVC应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在ASP.NET MVC应用程序Http request 和Http response 过程中, ...
- python中关于list列表的增删查改操作
python中list的操#python创建列表的时候,会以堆栈的形式存放数据,从右向左往堆栈中存放数据 movies=["The holy Grail","The li ...
- JDK环境变量配置及Tomcat安装服务
1.测试jdk安装是否成功: 在cmd中输入java -version 2.环境变量: 1)新建系统变量JAVA_HOME,如:D:\Program Files\Java\jdk1.8.0_60 2) ...
- BeanUtils--内省加强
BeanUtils就是一个处理Bean的工具包.内部也是使用内省.但对内省做了加强. Bean的set |get不用再成对出现 核心类: BeanUtils. 1.导包