【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 ...
随机推荐
- 编译本地64位版本的hadoop-2.6.0
官方提供的hadoop-2.x版本貌似都是32位的,在64位机子下使用可能会报错,最好使用官方提供的源码进行本地编译,编译成适合本地硬件环境的64位软件包. 关于native Hadoop是使用J ...
- 清北学堂模拟day6 兔子
[问题描述] 在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与其相连.换句话讲,这些兔子 ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)
* 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数 ...
- XML理解
XML:页面之间传递数据,跨平台传递,核心标签 HTML:超文本标记语言,核心标签 <xml version='1.0'>版本1.0<Nation> <one> & ...
- 牡丹江.2014k(构造)
K - Known Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭)
欧拉函数,用φ(n)表示 欧拉函数是求小于等于n的数中与n互质的数的数目 辣么,怎么求哩?~(-o ̄▽ ̄)-o 可以先在1到n-1中找到与n不互质的数,然后把他们减掉 比如φ(12) 把12质因数分解 ...
- ICloud没有密码怎么注销?
忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...
- javascript高级程序设计---CSS操作
CSS与JavaScript是两个有着明确分工的领域,前者负责页面的视觉效果,后者负责与用户的行为互动.但是,它们毕竟同属网页开发的前端,因此不可避免有着交叉和互相配合. HTML元素的style属性 ...
- Javascript高级程序设计——面向对象之创建对象
对象创建方法: 工厂方法 构造函数模式 原型模式 组合构造函数和原型模式 寄生构造函数模式 问题构造函数模式 工厂模式: function Person(name, age){ var obj = n ...