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)的更多相关文章

  1. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  4. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. 【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. 思路:不能用 ...

  7. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 【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 ...

  9. 【leetcode】Divide Two Integers (middle)☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. sql server cpu占用过高优化

    SQLSERVER排查CPU占用高的情况 今天中午,有朋友叫我帮他看一下数据库,操作系统是Windows2008R2 ,数据库是SQL2008R2 64位 64G内存,16核CPU 硬件配置还是比较高 ...

  2. MVC中你必须知道的13个扩展点

    MVC中你必须知道的13个扩展点 pasting 转:http://www.cnblogs.com/kirinboy/archive/2009/06/01/13-asp-net-mvc-extensi ...

  3. [译]git init

    git init git init命令用来创建一个新的Git仓储.可以用在一个已经存在的但是没有受Git版本控制的项目,或者用来初始化一个全新的没有任何文件的空仓储.git init通常是你开始一个新 ...

  4. windows2003最详细的安装操作步骤.(最详细)

    以下为windows2003的安装操作步骤,由于安装操作步骤较多,安装可能需要一定的实际安装经验.安装时请参照此文档一步步完成安装. 一.首先准备好Windows2003安装光盘CD1,将CD1光盘放 ...

  5. QT文件读写

    /* //文件读取 QFile f("c:\\t.txt"); if(!f.open(QIODevice::WriteOnly | QIODevice::Text)) { qDeb ...

  6. 【PHP面向对象(OOP)编程入门教程】5.如何实例化对象?

    我们上面说过面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,既然我们类会声明了,下一步就是实例化对象了. 当定义好类后,我们使用new关键字来生成一个对象. $对象名称 = new 类名称 ...

  7. 1.2 从 ACID 到 CAP/BASE

    1.事务 事务(Tranction)是指,由一系列对系统中数据进行访问与更新操作,所组成的一个逻辑执行单元.狭义上的事务是指数据库事务. 事务有四个特性. 原子性:原子性要求事务只允讲有两种状态,全部 ...

  8. springmvc之默认错误页面跳转

    在做一个项目的时候,为了界面美观及用户体验,我们往往会设计自己的错误跳转页面,而不是直接展示给用户一堆错误码,为此我们需要配置自己的错误跳转页面. 1.项目结构 2.web.xml <!DOCT ...

  9. 修改Mysql默认编码

    show variables like 'character%';+--------------------------+----------------------------+| Variable ...

  10. iOS开发初级课程

    iOS开发初级课程 针对学员 掌握Objective  C,C或者C++,有语言基础的学员,想从事iOS开发工作. iOS开发那些事-了解iOS开发(8集) 在课程中,我们首先介绍如何使用nib和故事 ...