200. Number of Islands (Graph)
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(意思是,如果仅仅是斜角都为1,不是ajacent land). 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,表示找到了一个island,然后找它的连通图,把这个连通图里的1都设置标志。
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int hight = grid.size();
if(hight == ) return ;
int width = grid[].size();
int ret = ;
for(int i = ; i < hight; i++){
for(int j = ; j < width; j++){
if(grid[i][j]=='') continue;
//find a land, continue to find land at its side
ret++;
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
}
return ret;
}
void dfs(vector<vector<char>>& grid, int i, int j){
int hight = grid.size();
int width = grid[].size();
if(i>=hight || j>= width || i< || j<) return;
if(grid[i][j]=='') return;
//find a land, continue to find land at its side
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
};
200. Number of Islands (Graph)的更多相关文章
- 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 用了第一种方式, ...
- 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
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 ...
- (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 ...
- 200. Number of Islands(DFS)
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 s ...
- [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 ...
随机推荐
- 【转】簡單講講 USB Human Interface Device
原地址http://213style.blogspot.com/2013/09/usb-human-interface-device.html 恩,發本文的原因是看到了以前畢業的朋友在旁邊的對話框問了 ...
- 基于Redis的Spring cache 缓存介绍
目录 Cache API及默认提供的实现 demo 依赖包安装 定义实体类.服务类和相关配置文件 Cache注解 启用Cache注解 @CachePut @CacheEvict @Cacheable ...
- 【JEECG技术文档】表单配置-树形表单
表单配置支持树型表单了,具体效果如下图: 配置说明: 1.是否树:选择是. 2.树形表单父Id:表的自关联外键. 3.树形表单列表:显示树形图标的列,如上图中为[组织机构名称]. 4.默认值:最外层数 ...
- 如何禁用Firefox,chrome浏览器“不安全密码警告”
在任何HTTP页面中,一个全新的“不安全密码警告”将会在用户点击表单时直接出现在登陆框的下方,强行保证所有用户都能看到“此链接不安全,你的个人利益将受到损害”等字眼,同时整个页面也会收到损坏的挂锁图标 ...
- 集合,ArrayList
用集合代替数组: Console.Write("请输入人数:"); int renshu = int.Parse(Console.ReadLine()); ArrayList ch ...
- cordova 源码分析记录
1.模块定义 (function () { var modules = {}; // Stack of moduleIds currently being built. var requireStac ...
- JSP基本
JSPとは.HTMLファイルにJavaコードを埋め込んでおき.クライアントの要求に応じてコードを実行.処理結果のみをクライアントに送信する技術です. 1.JSPは実はサーブレットです.最初にリクエスト ...
- Haskell语言学习笔记(87)Time
安装 time $ cabal install time Installed time-1.9.1 Prelude> import Data.Time Prelude Data.Time> ...
- sass实战演练01 - 外部文件引用和变量
SASS是什么? 目前前端开发中css已经是公认的”前端程序员必须掌握”的知识,最早的css编写都是手工一条条写出来的,工作量大.不利于维护. 而sass的存在使得css开发可以像写代码一样最终生成一 ...
- Linux各种类型压缩包解压缩方法
Linux上经常需要处理各种类型压缩包的解包和打包处理.通常来说会处理最常见的.tar包和.tar.gz包就能解决大部分问题了,但是真碰到其他类型的压缩包还是需要知道如何进行解压缩.这里对Linux上 ...