题目:

给一个01矩阵,求不同的岛屿的个数。

0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。

样例

在矩阵:

[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]

中有 3 个岛.

解题:

programcreek看到是根据深度优先算法

对某个位置(i,j)

当是1 的时候,是岛屿,该位置设为 0 ,并将四周的 1 设置为 0,这样就是递归思想了

当是0的时候,不是岛屿,寻找下一个位置

Java程序:

public class Solution {
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
public int numIslands(boolean[][] grid) {
// Write your code here
int m = grid.length;
if(m==0)
return 0;
int count = 0;
int n = grid[0].length;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==true){
dfs(grid,i,j);
count++;
}
}
}
return count;
}
private void dfs(boolean[][]grid,int i,int j){
if(i<0 || j<0||i>=grid.length || j>=grid[0].length)
return ;
if(grid[i][j]==true){
grid[i][j]= false;
dfs(grid,i-1,j);
dfs(grid,i+1,j);
dfs(grid,i,j-1);
dfs(grid,i,j+1);
} }
}

总耗时: 9193 ms

Python程序:

class Solution:
# @param {boolean[][]} grid a boolean 2D matrix
# @return {int} an integer
def numIslands(self, grid):
# Write your code here
m = len(grid)
if m==0:
return 0
n = len(grid[0])
if n==0:
return 0
count = 0
for i in range(m):
for j in range(n):
if grid[i][j]==True:
self.dfs(grid,i,j)
count +=1
return count def dfs(self,grid,i,j):
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]):
return
if grid[i][j]==True:
grid[i][j]=False
self.dfs(grid,i-1,j)
self.dfs(grid,i+1,j)
self.dfs(grid,i,j-1)
self.dfs(grid,i,j+1)

总耗时: 433 ms

lintcode:Number of Islands 岛屿的个数的更多相关文章

  1. [LeetCode] 0200. Number of Islands 岛屿的个数

    题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...

  2. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  3. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

  4. Leetcode200. Number of Islands岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  5. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  6. [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 ...

  7. [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 ...

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

  9. LeetCode 200. Number of Islands 岛屿数量(C++/Java)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

随机推荐

  1. Perl的主要应用领域

    1.Unix系统的维护功能    如我们在前面所说的,Perl可以作为传统Unix系统维护工具的替代,在这方面,它可以对文本文件,特别是对配置文件(还记不记得在配置Linux系统中的文本方式的配置)进 ...

  2. 【Sharing】如何成为一名黑客

    [声明]此文为转载,只为收藏. 从小到大听说了无数关于“电脑黑客”的故事,比如XXX入侵美国五角大楼,再比如前几年的“熊猫烧香”病毒,这些故事的主角都被我们的媒体称之为“黑客”.其实这些人,更大程度上 ...

  3. C# winform 中 TabControl 动态显示 TabPage

    在winform应用中,tabcontrol是一个很好的控件,可以根据需求提供多个选项卡(TabPages),但是有一个问题是当某个项目需要多个选项卡,但是不同的功能要求显示不同的选项卡,其他的非该功 ...

  4. Redis 一:安装篇

    .安装环境,虚拟机 + centos6. PS::前提已经安装了yum的情况下 第一步:安装 mkdir /usr/redis 新建redis目录 cd /usr/redis 进入目录 wget ht ...

  5. Global::validateEmail

    /***************************************************************** (C) Copyright DENTSPLY Internatio ...

  6. CHARINDEX,PATINDEX,STUFF函数

    -- CHARINDEX函数 -- 返回字符或者字符串在另一个字符串中的起始位置. -- 语法:CHARINDEX(expression1 , expression2 [,start_location ...

  7. ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

    OS: centos 6.3DB: 5.5.14 测试创建yoon测试表,没有主键,没有索引,基础数据内容如下: mysql> select * from yoon;+----+-------- ...

  8. python之input(), raw_input()

    input(): 要求输入合法的python表达式, 例如字串需要加"", 四则运算会自动计算. raw_input():所有输入视作字串 >>> val=inp ...

  9. mysql 连接语句

    在 SELECT 语句中,如果 FROM 子句引用了多个表源或视图,可以使用 JOIN 指示指定的联接操作应在指定的表源或视图之间执行. 一.交叉联接:CROSS JOIN 交叉联接将执行一个叉积(迪 ...

  10. inputstream和outputstream读写数据模板代码

    //读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(b ...