★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝()➤GitHub地址:➤原文地址: ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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:11110110101100000000Output: 1
Example 2:
Input:11000110000010000011Output: 3
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:11110110101100000000Output: 1
Example 2:
Input:11000110000010000011Output: 3
给定一个由 '1'
(陆地)和 '0'
(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入:11110110101100000000输出: 1
示例 2:
输入:11000110000010000011输出: 3
260ms
1 class Solution { 2 func numIslands(_ grid: [[Character]]) -> Int { 3 var map = grid 4 let row = map.count 5 if row == 0 { return 0} 6 let col = map[0].count 7 var count = 0 8 for i in 0..= 0 && i < row && j >= 0 && j < col && map[i][j] == "1") {20 map[i][j] = "2"21 dfs(&map, i, j-1, row, col);22 dfs(&map, i-1, j, row, col);23 dfs(&map, i, j+1, row, col);24 dfs(&map, i+1, j, row, col);25 }26 }27 }
268ms
1 class Solution { 2 func numIslands(_ grid: [[Character]]) -> Int { 3 var map = grid 4 let row = map.count 5 if row == 0 { return 0} 6 let col = map[0].count 7 var count = 0 8 for i in 0..= 0 && i < row && j >= 0 && j < col && map[i][j] == "1") {20 map[i][j] = "2"21 let index = [0,-1,0,1,0]22 for k in 0..<(index.count - 1) {23 dfs(&map, i + index[k], j + index[k + 1], row, col)24 }25 }26 }27 }
288ms
1 class Solution { 2 func numIslands(_ grid: [[Character]]) -> Int { 3 var grid = grid 4 var count = 0 5 6 for row in grid.indices { 7 for col in 0..0, grid[x - 1][y] == "1" {34 grid[x - 1][y] = "0" // mark as visited35 queue.append((x - 1, y))36 }37 38 //down part39 if x < grid.count - 1, grid[x + 1][y] == "1" {40 grid[x + 1][y] = "0" // mark as visited41 queue.append((x + 1, y))42 }43 44 //left part45 if y > 0, grid[x][y - 1] == "1" {46 grid[x ][y - 1] = "0" // mark as visited47 queue.append((x, y - 1))48 }49 50 //right part51 if y < grid[x].count - 1, grid[x][y + 1] == "1" {52 grid[x ][y + 1] = "0" // mark as visited53 queue.append((x, y + 1))54 }55 }56 }57 58 func dfs(grid: inout [[Character]], row: Int, col: Int) {59 //dfs: pre order tree: me left righ => me, up, down, left right60 61 // me step62 guard grid[row][col] == "1" else { return }63 64 grid[row][col] = "0"// mark as visited, smart65 66 //up part67 if row > 0, grid[row - 1][col] == "1" {68 dfs(grid: &grid, row: row - 1, col: col)69 }70 71 //down part72 if row < grid.count - 1, grid[row + 1][col] == "1" {73 dfs(grid: &grid, row: row + 1, col: col)74 }75 76 //left part77 if col > 0, grid[row][col - 1] == "1" {78 dfs(grid: &grid, row: row, col: col - 1)79 }80 81 //right part82 if col < grid[row].count - 1, grid[row][col + 1] == "1" {83 dfs(grid: &grid, row: row, col: col + 1)84 }85 86 }87 }
292ms
1 class Solution { 2 func numIslands(_ grid: [[Character]]) -> Int { 3 guard !grid.isEmpty, !grid[0].isEmpty else { return 0 } 4 5 var queue = [[Int]]() 6 var grid = grid 7 var count = 0 8 9 for i in 0..= grid.count || column < 0 || column >= grid[0].count || grid[row][column] == "0" {32 continue33 }34 grid[row][column] = "0"35 queue.append([row, column])36 }37 }38 }39 }
296ms
1 class Solution { 2 func numIslands(_ grid: [[Character]]) -> Int { 3 guard grid.count > 0, grid[0].count > 0 else { 4 return 0 5 } 6 var result = 0 7 8 var dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] 9 10 var memo = [[Bool]](repeating:[Bool](repeating:false, count:grid[0].count), count:grid.count)11 12 func dfs(i:Int, j:Int) {13 if i < 0 || j < 0 || i >= grid.count || j >= grid[0].count || memo[i][j] == true || grid[i][j] == "0" {14 return15 }16 17 memo[i][j] = true18 19 for dir in dirs {20 dfs(i:i + dir[0], j: j + dir[1])21 }22 }23 24 for i in 0..
452ms
1 class Solution { 2 3 func numIslands(_ grid: [[Character]]) -> Int { 4 5 var map = grid 6 let n = map.count; 7 if n == 0 { return 0 } 8 let m = map[0].count 9 var count = 010 11 func emptMap(_ map: inout [[Character]], i: Int, j: Int) {12 if (i < 0 || j<0 || i>=n || j>=m || map[i][j] != "1") { return }13 map[i][j] = "0";14 // 递归清除所有地点15 emptMap(&map, i: i+1, j: j)16 emptMap(&map, i: i-1, j: j)17 emptMap(&map, i: i, j: j+1)18 emptMap(&map, i: i, j: j-1)19 }20 21 for (i,v) in map.enumerated() {22 for (j,_) in v.enumerated () {23 if (map[i][j] == "1") {24 emptMap(&map, i: i, j: j)25 count = count + 126 }27 }28 }29 30 return count31 }32 }