lintcode 容易题:Number of Islands 岛屿的个数

题目:

岛屿的个数

给一个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 个岛.

解题:

网上看到是根据深度优先算法,和递归思想进行解决的题目,当发现这一点是1的时候,将周围的点都设置成 0,如何设置成 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

时间: 2024-10-05 14:00:16

lintcode 容易题:Number of Islands 岛屿的个数的相关文章

200 Number of Islands 岛屿的个数

给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000000答案: 1示例 2:11000110000010000011答案: 3 详见:https://leetcode.com/problems/number-of-islands/description/ class Solution { public: int numIslands(vector<

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode 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 v

[LintCode] Number of Islands 岛屿的数量

Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Have you met this ques

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

LintCode Python 简单级题目 433.岛屿的个数

题目描述: 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 您在真实的面试中是否遇到过这个题? Yes 样例 在矩阵: [ [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 个岛. 标签 脸书 谷歌 Zenefits 题目分析: 循环2维数组,找到其值为1的元素,count++, 然后递

lintcode 中等题:Divide Two Integers 两个数的除法

题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题  15%的通过率,减法,位运算?表示不知道如何下手. 法一:利用减法,超时,人工直接去除的一些情况太流氓. public class Solution { /** * @param dividend the dividend * @param divisor the divisor * @return the re

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

Number of Islands II

Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y

Number of Islands——LeetCode

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