成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[Leetcode] Number of Islands 島嶼個數(shù)

Raaabbit / 2574人閱讀

摘要:同時我們每找到一個,就將其標為,這樣就能把整個島嶼變成。我們只要記錄對矩陣遍歷時能進入多少次搜索,就代表有多少個島嶼。

Number of Islands 最新更新的思路,以及題II的解法請訪問:https://yanjia.me/zh/2018/11/...

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

標零法 復雜度

時間 O(NM) 空間 O(max(N,M)) 遞歸??臻g

思路

我們遍歷矩陣的每一個點,對每個點都嘗試進行一次深度優(yōu)先搜索,如果搜索到1,就繼續(xù)向它的四周搜索。同時我們每找到一個1,就將其標為0,這樣就能把整個島嶼變成0。我們只要記錄對矩陣遍歷時能進入多少次搜索,就代表有多少個島嶼。

代碼
public class Solution {
    public int numIslands(char[][] grid) {
        int res = 0;
        if(grid.length==0) return res;
        for(int i = 0; i < grid.length; i++){
            for(int j = 0; j < grid[0].length; j++){
                if(grid[i][j]=="1"){
                    searchIsland(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    
    private void searchIsland(char[][] grid, int i, int j){
        grid[i][j]="0";
        // 搜索該點連通的上下左右
        if(i>0 && grid[i-1][j]=="1") searchIsland(grid, i-1, j);
        if(j>0 && grid[i][j-1]=="1") searchIsland(grid, i, j-1);
        if(i

2018/2

class Solution:
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        count = 0
        for row in range(0, len(grid)):
            for col in range(0, len(grid[0])):
                if grid[row][col] == "1":
                    self.exploreIsland(grid, row, col)
                    count += 1
        return count
                    
    
    def exploreIsland(self, grid, row, col):
        grid[row][col] = "0"
        if (row > 0 and grid[row - 1][col] == "1"):
            self.exploreIsland(grid, row - 1, col)
        if (col > 0 and grid[row][col - 1] == "1"):
            self.exploreIsland(grid, row, col - 1)
        if (row < len(grid) - 1 and grid[row + 1][col] == "1"):
            self.exploreIsland(grid, row + 1, col)
        if (col < len(grid[0]) - 1 and grid[row][col + 1] == "1"):
            self.exploreIsland(grid, row, col + 1)

2018/10

func explore(grid *[][]byte, row int, col int) {
    (*grid)[row][col] = "0"
    if row > 0 && (*grid)[row-1][col] == "1" {
        explore(grid, row-1, col)
    }
    if row < len((*grid))-1 && (*grid)[row+1][col] == "1" {
        explore(grid, row+1, col)
    }
    if col > 0 && (*grid)[row][col-1] == "1" {
        explore(grid, row, col-1)
    }
    if col < len((*grid)[0])-1 && (*grid)[row][col+1] == "1" {
        explore(grid, row, col+1)
    }
}

func numIslands(grid [][]byte) int {
    count := 0
    for i, row := range grid {
        for j, val := range row {
            if val == "1" {
                explore(&grid, i, j)
                count++
            }
        }

    }
    return count
}
后續(xù) Follow Up

Q:如何找湖的數(shù)量呢?湖的定義是某個0,其上下左右都是同一個島嶼的陸地。
A:我們可以先用Number of island的方法,把每個島標記成不同的ID,然后過一遍整個地圖的每個點,如果是0的話,就DFS看這塊連通水域是否被同一塊島嶼包圍,如果出現(xiàn)了不同數(shù)字的陸地,則不是湖。

public class NumberOfLakes {
    
    public static void main(String[] args){
        NumberOfLakes nof = new NumberOfLakes();
        int[][] world = {{1,1,1,0,1},{1,0,0,1,0},{1,1,1,1,1,},{0,1,1,0,1},{0,1,1,1,1}};
        System.out.println(nof.numberOfLakes(world));
    }
    
    public int numberOfLakes(int[][] world){
        int islandId = 2;
        for(int i = 0; i < world.length; i++){
            for(int j = 0; j < world[0].length; j++){
                if(world[i][j] == 1){
                    findIsland(world, i, j, islandId);
                    islandId++;
                }
            }
        }
        int lake = 0;
        for(int i = 0; i < world.length; i++){
            for(int j = 0; j < world[0].length; j++){
                if(world[i][j] == 0){
                    // 找到當前水域的鄰接陸地編號
                    int currId = 0;
                    if(i > 0) currId = (world[i - 1][j] != 0 ? world[i - 1][j] : currId);
                    if(j > 0) currId = (world[i][j - 1] != 0 ? world[i][j - 1] : currId);
                    if(i < world.length - 1) currId = (world[i + 1][j] != 0 ? world[i + 1][j] : currId);
                    if(j < world[0].length - 1) currId = (world[i][j + 1] != 0 ? world[i][j + 1] : currId);
                    // 如果該點是湖,加1
                    if(findLake(world, i, j, currId)){
                        lake++;
                    }
                }
            }
        }
        return lake;
    }
    
    private boolean findLake(int[][] world, int i, int j, int id){
        // 將當前水域標記成周邊島嶼的數(shù)字
        world[i][j] = id;
        // 找上下左右是否是同一塊島嶼的陸地,如果是水域則繼續(xù)DFS,如果當前水域是邊界也說明不是湖
        boolean up = i != 0 
                && (world[i - 1][j] == id 
                || (world[i - 1][j] == 0 && findLake(world, i - 1, j, id)));
        boolean down = i != world.length - 1 
                && (world[i + 1][j] == id 
                || (world[i + 1][j] == 0 && findLake(world, i + 1, j, id)));
        boolean left = j != 0 
                && (world[i][j - 1] == id 
                || (world[i][j - 1] == 0 && findLake(world, i, j - 1, id)));
        boolean right = j != world[0].length - 1 
                && (world[i][j + 1] == id 
                || (world[i][j + 1] == 0 && findLake(world, i, j + 1, id)));
        return up && down && right && left;
    }
    
    private void findIsland(int[][] world, int i, int j, int id){
        world[i][j] = id;
        if(i > 0 && world[i - 1][j] == 1) findIsland(world, i - 1, j, id);
        if(j > 0 && world[i][j - 1] == 1) findIsland(world, i, j - 1, id);
        if(i < world.length - 1 && world[i + 1][j] == 1) findIsland(world, i + 1, j, id);
        if(j < world[0].length - 1 && world[i][j + 1] == 1) findIsland(world, i, j + 1, id);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/64546.html

相關(guān)文章

  • [Leetcode] Number of Islands 島嶼數(shù)量(JavaScript 實現(xiàn))

    摘要:解題思路標零法對這個矩陣進行循環(huán)訪問每一個點當這個點等于,島嶼數(shù)量,與其同時用算法訪問周圍的其他點,進行同樣的操作且將訪問過且等于的點標記為零。版本島嶼數(shù)量搜索右邊搜索左邊搜索下邊搜索上邊 Q: Number of Islands Given a 2d grid map of 1s (land) and 0s (water), count the number of islands. ...

    pingan8787 評論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個循環(huán)遍歷整個矩陣,出現(xiàn)則將其周圍相鄰的全部標記為,用子函數(shù)遞歸標記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 Number of Islands Problem Given a boolean/char 2D matrix, find the number of islands. 0 is represented as the sea, 1 is represented as...

    Fourierr 評論0 收藏0
  • [LeetCode] 694. Number of Distinct Islands

    Problem Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...

    SunZhaopeng 評論0 收藏0
  • leetcode200. Number of Islands

    摘要:題目要求提供一個二維數(shù)組表示一張地圖,其中代表陸地,代表海洋。這里使用一個新的二維數(shù)組來表示對應(yīng)地圖上的元素屬于哪個并查集。在合并的時候先進行判斷,如果二者為已經(jīng)相連的陸地,則無需合并,否則將新的二維數(shù)組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...

    Zoom 評論0 收藏0
  • 前端 | 每天一個 LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學習。 這篇文章記錄我練習的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...

    張漢慶 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<