[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 ...
随机推荐
- 第一篇!in和exists性能比较和使用
首先,先看下in和exists的区别: in 是把外表和内表作hash 连接: exists是对外表作loop循环,每次loop循环再对内表进行查询. 普遍的观点是exists比in效率高的.但是这不 ...
- 配置ISCSI服务器
一.在linux下安装启动iscsi target 1.安装启动iscsi服务 [root@wjb10000 ~]# yum -y install targetcli.noarch 2.建立一个目录设 ...
- IO-文件 File 复制 读写 总结
一定要注意: 传入的参数,应该是包含文件名的完整路径名,不能把一个文件复制到[文件夹]中,因为[文件夹]本身是不能有输入输出流的,只能复制到一个[文件]中,否则会报异常. 以字节流读写的三种方式 pu ...
- Android布局文件-错误
View requires API level 14 (current min is 8): <?xml version="1.0" encoding="utf-8 ...
- spring-qualifier解释
如果一个class有两个对应的beanId,在Autowired的时候,必须指定Qualifier("指定其中一个beanId"). org.springframework.bea ...
- 大型系统开发sql优化总结(转)
Problem Description: 1.每个表的结构及主键索引情况 2.每个表的count(*)记录是多少 3.对于创建索引的列,索引的类型是什么?count(distinct indexcol ...
- Nginx配置文件nginx.conf详解(转)
#定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...
- 对于没有Command属性时,怎么来达到相同的效果
控件是第三方Telerik控件 CellEditEnded事件想写成Command{Binding CellEditEndedCommand} 这样的效果如下代码 需要引用 System.Wind ...
- Visual Studio使用技巧记录
1.关闭调试,iis express仍显示在托盘中: 工具 ---> 选项 ---> 调试 ---> 编辑并继续,取消选择“编辑并继续”的选择框 2.关闭浏览器一直请求: 在调试旁边 ...
- log4j日志输出使用教程
Log4j是帮助开发人员进行日志输出管理的API类库.它最重要的特点就可以配置文件灵活的设置日志信息的优先级.日志信息的输出目的地以及日志信息的输出格式.Log4j除了可以记录程序运行日志信息外还有一 ...