题目:

给一个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. 转:Java HashMap实现详解

    Java HashMap实现详解 转:http://beyond99.blog.51cto.com/1469451/429789 1.    HashMap概述:    HashMap是基于哈希表的M ...

  2. 通信协议之HTTP,UDP,TCP协议

    1.UDP,TCP,HTTP之间的关系 tcp/ip是个协议组,它可以分为4个层次,即网路接口层,网络层,传输层,以及应用层, 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协 ...

  3. 51nod 1021 石头归并

    1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合 ...

  4. WordPress 主题开发 - (三) 开发工具 待翻译

    Before we get started building any WordPress Theme, we’re going to need to get our development tools ...

  5. Microsoft Visual Studio Ultimate 2015 Preview使用笔记

    1.内存好象存在泄露问题

  6. JDBC 连接数据库

          JAVA使用JDBC访问数据库的步骤: 1.     得到数据库驱动程序   (导包) 2.     创建数据库连接  3.     执行SQL语句 4.     得到结果集 5.     ...

  7. Visual Studio

    1.必备神器http://www.cnblogs.com/stoneniqiu/p/3488546.html 2.常用快捷键http://www.cnblogs.com/TankXiao/p/3468 ...

  8. thinkphp中curl的使用,常用于接口

    /lib/action/PublicAction.class.php class PublicAction extends Action{ //curl,返回数组 public function ge ...

  9. matlab实现共轭梯度法、多元牛顿法、broyden方法

    共轭梯度法: function [ x, r, k ] = CorGrant( x0, A, b ) x = x0; r = b - A * x0; d = r; X = ones(length(x) ...

  10. 第一章 Web MVC简介

    Web MVC简介 1.1.Web开发中的请求-响应模型: 在Web世界里,具体步骤如下: 1.  Web浏览器(如IE)发起请求,如访问hao123主页 2.  Web服务器(如Tomcat)接收请 ...