题目:

给一个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. Ubuntu16.04.1 安装Redis-Cluster

    Redis在3.0版正式引入了集群这个特性.Redis集群是一个分布式(distributed).容错(fault-tolerant)的 Redis内存K/V服务, 集群可以使用的功能是普通单机 Re ...

  2. 51nod贪心算法入门-----完美字符串

    约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同. ...

  3. c# datatable list 相互转换

    /*Converts List To DataTable*/ public static DataTable ToDataTable<TSource>(IList<TSource&g ...

  4. 因修改system密码导致expdp备份失败

    今天发现一套系统的逻辑备份失效了,检查了一下,发现主要是由于之前其他管理员修改了system用户的密码,导致备份不成功了.为了今后此类的问题发生,修改expdp的脚本连接部分如下:expdp \' / ...

  5. 【经验】Angularjs 中使用 layDate 日期控件

    layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...

  6. android 中使用回调方法(适用于自定义view传值到activity、adapter传值到activity)

    如图所示: 每当listview中有选中的操作时都需要发消息给activity,用来实时改变真实需要支付的剩余金额. 代码暂不公开啦!公司项目!

  7. 简单制作mib表

    今天放假后第一天上班,将假前自学制作mib表的东西说一下. 在这里呢,我以世界-中国-上海-闵行这种包含关系介绍,感觉更容易理解. MIB file的开始和结束 所有的MIB file的都以DEFIN ...

  8. HTML+CSS入门

    <strong>加粗</strong> <em>斜体</em> <p>段落</p> <span>设置单独样式< ...

  9. clion 帮助文档 EN

    下载时间 2015年10月 下载地址:http://pan.baidu.com/s/1E4fgE 备用地址:链接:http://pan.baidu.com/s/1bn6u5Wj 密码:icn4

  10. sqlserver 动态表名 动态字段名 执行 动态sql

    动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...