[leetcode] 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:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
Special thanks to @mithmatt for adding this problem and creating all test cases.
class Solution
{
private:
void dfs(vector<vector<char> > &grid, int x, int y)
{
if(x < || x >= grid.size()) return;
if(y < || y >= grid[].size()) return;
if(grid[x][y] != '') return; grid[x][y] = 'x';
dfs(grid, x-, y);
dfs(grid, x+, y);
dfs(grid, x, y-);
dfs(grid, x, y+);
}
public:
int numIslands(vector<vector<char> > &grid)
{
if(grid.empty() || grid[].empty()) return ;
int X = grid.size(), Y = grid[].size();
int count = ; for(int i=; i<X; i++)
{
for(int j=; j<Y; j++)
{
if(grid[i][j] == '')
{
count++;
dfs(grid, i, j);
}
}
} return count;
}
};
[leetcode] Number of Islands的更多相关文章
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 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: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- LeetCode – Number of Islands II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- LeetCode – Number of Islands
Given a -d grid map of 's (water), count the number of islands. An island is surrounded by water and ...
- LeetCode Number of Islands 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- 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] Number of Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- java攻城师之路--复习java web之jsp入门_El表达式_JSTL标签库
JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...
- ArcGIS提取影像边界
基于ArcGIS有多重办法可以提取影像边界,比如常用的有以下几种方式: a.System Toolboxes --> 3D Analyst Tools --> Conversion --& ...
- ruby -- 进阶学习(九)定制错误跳转404和500
在开发阶段,如果发生错误时,都会出现错误提示页面,比如:RecordNotFound之类的,虽然这些错误方便开发进行debug,但是等产品上线时,如果还是出现这些页面,对于用户来说是很不友好的. 所以 ...
- Tools - Oracle SQL Developer
Oracle SQL Developer Oracle公司出品的一个免费非开源的用以开发数据库应用程序的图形化工具. 可以浏览数据库对象.运行SQL语句和脚本.编辑和调试PL/SQL语句,也可以创建执 ...
- LeetCode——Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- C#语法糖之Cookies操作类 asp.net
用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&quo ...
- [转]easyui datagrid 批量编辑和提交
web前台主要代码: <script type="text/javascript"> $(function() { var $dg = $("#dg" ...
- C#中override和new修饰符的区别
(new)“隐藏”,(override)“覆盖”(重写).不过要弄清楚这两个有什么区别确实也很难,因为子类在使用父类方法时根本看不出区别,子类不管父类是new了还是override了,用的都是父类方法 ...
- 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink
[源码下载] 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink 作者:webabcd 介绍重新想象 Windows 8.1 Store A ...
- No.001:Two Sum
问题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...