[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 ...
随机推荐
- 【软件分析与挖掘】Multiple kernel ensemble learning for software defect prediction
摘要: 利用软件中的历史缺陷数据来建立分类器,进行软件缺陷的检测. 多核学习(Multiple kernel learning):把历史缺陷数据映射到高维特征空间,使得数据能够更好地表达: 集成学习( ...
- annotation:@Override出现The method of type must override asuperclass解决方案
原因追踪及解决办法: 1. 查阅资料发现说在jdk1.5下要使用@Override这个annotation必须保证被标注的方法来源于class而不是interface. 2. 即使自己的jdk是1.6 ...
- sass调试--页面看到sass文件而不是css文件问题
在浏览器页面有时看到sass文件而不是css文件问题,其主要由于sass开启了source-map(调试)功能,问题如下图: sass调试 sass调试需要开启编译时输出调试信息和浏览器调试功能,两者 ...
- DDD:一个朋友对领域驱动的小结
首先我在一家老板有点关系的小软件公司带领一帮工作一两年的程序员做项目,这里要特别强调的是做项目(差不多是外包,只不过客户群体比较固定),项目就是今天项目A是这个逻辑,明天项目B是那个逻辑,两者之间的业 ...
- Nginx: could not build the server_names_hash 解决办法
# /etc/init.d/nginx reload * Reloading nginx configuration nginx [fail] # nginx -t nginx: [emerg] co ...
- 通过组策略实现IE自动以当前域账号登录某站点
SharePoint基于windows验证的如何通过组策略实现IE自动以当前域账号登录某站点 1. 在运行中运行MMC,启动“组策略对象编辑器”. 如下图: 2.找到组策略,如下图: 3.找到对应的域 ...
- 15款提高工作效率的 Web 项目管理工具
在今天的快节奏的商业世界里,能够通过计划.组织.和管理资源池以及评估开发资源的模式来管理一个项目,是一个很艰巨的任务. 有很多现成的项目管理软件来帮助减轻项目管理的负担,并且他们几乎覆盖了所有类型的业 ...
- .NET框架面向对象分层的个人想理
简单.层次清晰不要过度优化,接口这玩意儿就是个双刃剑,玩好了解藕,玩不好自找麻烦,好的代码永远都是傻瓜都能看懂的. 总结成以下几条: 公用层 代码公用并且与第三方DLL和业务逻辑无关的 独立出来 逻辑 ...
- STL --- UVA 123 Searching Quickly
UVA - 123 Searching Quickly Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...
- C# WinForm 技巧八:界面开发之“WeifenLuo.WinFormsUI.Docking+OutLookBar” 使用
概述 转自 http://www.cnblogs.com/luomingui/archive/2013/09/19/3329763.html 最近几天一直在关注WinFrom方面的文章 有想着提炼一下 ...