【leetcode】Number of Islands(middle)
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
思路:与Surrounded Regions(middle)☆的思路是一样的,也可以用广度和深度优先搜索。当遇到1的时候,把区域数加1,并把与这个1相连的整片区域标记为2.
下面的BFS用时25ms,DFS用时16ms
int numIslands(vector<vector<char>>& grid) {
if(grid.empty()) return ;
int num = ;
for(int i = ; i < grid.size(); ++i)
for(int j = ; j < grid[].size(); ++j)
if(grid[i][j] == '')
{
num++;
BFS(grid, i, j);
}
return num;
}
void BFS(vector<vector<char>>& grid, int r, int c)
{
queue<pair<int, int>> q;
q.push(make_pair(r, c));
while(!q.empty())
{
int i = q.front().first;
int j = q.front().second;
q.pop();
if(i >= && j >= && i < grid.size() && j < grid[].size() && grid[i][j] == '')
{
grid[i][j] = ;
q.push(make_pair(i - , j));
q.push(make_pair(i + , j));
q.push(make_pair(i, j - ));
q.push(make_pair(i, j + ));
}
}
}
void DFS(vector<vector<char>>& grid, int r, int c)
{
if(r >= && c >= && r < grid.size() && c < grid[].size() && grid[r][c] == '')
{
grid[r][c] = ;
DFS(grid, r - , c);
DFS(grid, r + , c);
DFS(grid, r, c - );
DFS(grid, r, c + );
}
}
很奇怪的是,开始我写的时候BFS多传了一个变量就TLE了??为什么呢??
int numIslands(vector<vector<char>>& grid) {
if(grid.empty()) return ;
int num = ;
for(int i = ; i < grid.size(); ++i)
{
for(int j = ; j < grid[].size(); ++j)
{
if(grid[i][j] == '')
BFS(grid, i, j, num);
}
}
return num;
}
void BFS(vector<vector<char>>& grid, int r, int c, int &num)
{
num++;
queue<pair<int, int>> q;
q.push(make_pair(r, c));
while(!q.empty())
{
int i = q.front().first;
int j = q.front().second;
q.pop();
if(i >= && j >= && i < grid.size() && j < grid[].size() && grid[i][j] == '')
{
grid[i][j] = num + ;
q.push(make_pair(i - , j));
q.push(make_pair(i + , j));
q.push(make_pair(i, j - ));
q.push(make_pair(i, j + ));
}
}
}
【leetcode】Number of Islands(middle)的更多相关文章
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】 search Insert Position(middle)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 再读<<基于MVC的JavaScript Web 富应用开发>>
工作的时候粗读过这本书的几章内容,真真是囫囵吞枣~~目前手边就剩这一本,重新读才觉得先前是没看明白啊!这个作者博闻强识,对这些插件.库了解的非常多.记录下,查的资料 订阅/发布 jQuery Tiny ...
- WCF-复合类型使用;传输图片
一:WCF服务端 IService1.cs中: public interface IService1 { [OperationContract] [WebInvoke(Method = "P ...
- java 练手 谁是最好的Coder
Problem A 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...
- PHP中PSR-[0-4]代码规范
PHP-FIG 在说啥是PSR-[0-4]规范的之前,我觉得我们有必要说下它的发明者和规范者:PHP-FIG,它的网站是:www.php-fig.org.就是这个联盟组织发明和创造了PSR-[0-4] ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- C\C++ sizeof 陷阱&&总结
今天使用动态数组,本来想通过sizeof 获取动态数据,结果出现了错误. 先对自己做个测试,能做出下面这个题目,并做出合理解释,可以不用往下看了. ][]; cout<< cout< ...
- JavaScript判断图片是否加载完成的三种方式
JavaScript判断图片是否加载完成的三种方式 有时需要获取图片的尺寸,这需要在图片加载完成以后才可以.有三种方式实现,下面一一介绍. 一.load事件 1 2 3 4 5 6 7 8 9 10 ...
- Unity路径规划
Unity路径规划 转自:http://www.cnblogs.com/zsb517/p/4090629.html 背景 酷跑游戏中涉及到弯道.不规则道路. 找来一些酷跑游戏的案例来看,很多都是只有 ...
- Android学习笔记(七)——常见控件
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...
- Java反射-方法(Method)
工作了三年,第二次使用反射! 遇到的问题描述: 多个页面查询后,返回的List中的对象属性为“.00”,页面显示不友好. 查询原因是因为查询数据的SQL为:to_char(a.applyAmount, ...