Leetcode - search-suggestions-system
Leetcode - 01-matrix

Leetcode - number-of-islands

violet posted @ Feb 29, 2020 07:05:42 AM in 算法 with tags Algorithm BFS Golang ruby , 329 阅读

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:

Input:
11110
11010
11000
00000

Output: 1

Example 2:

Input:
11000
11000
00100
00011

 

Use a walk method to do recursion in the grid. Once the grid is '1', islands count +1, and then mark it as '2' and then go as up, down, left, right directions. If i, j are in the edge, return 0.

func numIslands(grid [][]byte) int {
    if len(grid) == 0 {
        return 0
    }
    
    result := 0
    for i := 0; i < len(grid); i++ {
        for j := 0; j < len(grid[i]); j++ {
            result += walk(grid, i, j)
        }
    }

    return result
}

func walk(grid [][]byte, i, j int) int {
    if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[0]) {
        return 0
    }
    
    if grid[i][j] == '1' {
        grid[i][j] = '2'
        walk(grid, i+1, j)
        walk(grid, i-1, j)
        walk(grid, i, j-1)
        walk(grid, i, j+1)
        return 1
    }
    return 0
}

 

# @param {Character[][]} grid
# @return {Integer}
def num_islands(grid)
    return 0 if grid.length == 0 || grid[0].length == 0
    count = 0
    for i in 0..grid.length-1
        for j in 0..grid[i].length-1
            count += walk(grid, i, j)
        end
    end
    
    return count
end

def walk(grid, i, j)
    if i < 0 || j < 0 || i >= grid.length || j >= grid[0].length
        return 0
    end
    while grid[i][j] == '1'
        grid[i][j] = '2'
        walk(grid, i, j+1)
        walk(grid, i, j-1)
        walk(grid, i+1, j)
        walk(grid, i-1, j)
        return 1
    end
    return 0
end
BSEB Question Paper 说:
Sep 06, 2022 06:20:20 PM

Bihar Board Model Paper 2023 Class 5 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. BSEB Question Paper Class 5 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

startbildschirm layo 说:
Jul 18, 2023 07:34:13 PM

Wenn das Layout des Startbildschirms gesperrt ist, werden Änderungen an jedem Startbildschirm verhindert, nicht nur am ersten oder Standardbildschirm. Sie können weiterhin zwischen Startbildschirmen navigieren; das Gerät wird dadurch nicht gesperrt. startbildschirm layout entsperren Wenn Sie neue Apps installieren, werden jedoch keine Symbole auf Ihrem Startbildschirm platziert. Bis Ihr Startbildschirm entsperrt ist, sieht es so aus.

Goa 1st Class Textb 说:
Jul 26, 2023 06:49:18 PM

SCERT Goa Board Follows NCERT Curriculum These Textbooks are Updated as per the Syllabus Prescribed by NCERT. Students of 1st Class Should follow Prescribed Textbooks while Preparing for Exam. Our Team Refer to the Respective Subject Textbook while Preparing the Final Important questions. Goa Students Best Practice Study Materiel about Textbooks are the Fact that they are so Comprehensible Goa 1st Class Textbook 2024 that it does not require the aid of a Subject Literate.State Council of Educational Research and Training Goa Regular organization Primary School Education, Directorate Of Education Goa Published and Distribution Class Textbook Every Year.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter