[Swift]LeetCode200.岛屿的个数 | Number of Islands
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10180994.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 Output: 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: 11110 11010 11000 00000 Output: 1
Example 2:
Input: 11000 11000 00100 00011 Output: 3
给定一个由 '1'
(陆地)和 '0'
(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入: 11110 11010 11000 00000 输出: 1
示例 2:
输入: 11000 11000 00100 00011 输出: 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..<row { 9 for j in 0..<col { 10 if map[i][j] == "1" { 11 dfs(&map, i, j, row, col) 12 count += 1 13 } 14 } 15 } 16 return count 17 } 18 func dfs(_ map: inout [[Character]], _ i: Int, _ j: Int, _ row: Int, _ col: Int) { 19 if (i >= 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..<row { 9 for j in 0..<col { 10 if map[i][j] == "1" { 11 dfs(&map, i, j, row, col) 12 count += 1 13 } 14 } 15 } 16 return count 17 } 18 func dfs(_ map: inout [[Character]], _ i: Int, _ j: Int, _ row: Int, _ col: Int) { 19 if (i >= 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..<grid[row].count where grid[row][col] == "1" { //find the first point of contact 8 9 //dfs(grid: &grid, row: row, col: col) 10 11 bfs(grid: &grid, row: row, col: col) 12 13 count = count + 1 // found an island 14 } 15 } 16 17 return count 18 } 19 20 func bfs(grid: inout [[Character]], row: Int, col: Int) { 21 // bfs 只call了一次,所以可以直接默认进来的是1 22 var queue = [(Int, Int)]() 23 queue.append((row, col)) 24 grid[row][col] = "0" 25 26 while !queue.isEmpty { 27 let pair = queue.removeFirst() //don't use dropFirst 28 29 let x = pair.0 30 let y = pair.1 31 32 //up part 33 if x > 0, grid[x - 1][y] == "1" { 34 grid[x - 1][y] = "0" // mark as visited 35 queue.append((x - 1, y)) 36 } 37 38 //down part 39 if x < grid.count - 1, grid[x + 1][y] == "1" { 40 grid[x + 1][y] = "0" // mark as visited 41 queue.append((x + 1, y)) 42 } 43 44 //left part 45 if y > 0, grid[x][y - 1] == "1" { 46 grid[x ][y - 1] = "0" // mark as visited 47 queue.append((x, y - 1)) 48 } 49 50 //right part 51 if y < grid[x].count - 1, grid[x][y + 1] == "1" { 52 grid[x ][y + 1] = "0" // mark as visited 53 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 right 60 61 // me step 62 guard grid[row][col] == "1" else { return } 63 64 grid[row][col] = "0"// mark as visited, smart 65 66 //up part 67 if row > 0, grid[row - 1][col] == "1" { 68 dfs(grid: &grid, row: row - 1, col: col) 69 } 70 71 //down part 72 if row < grid.count - 1, grid[row + 1][col] == "1" { 73 dfs(grid: &grid, row: row + 1, col: col) 74 } 75 76 //left part 77 if col > 0, grid[row][col - 1] == "1" { 78 dfs(grid: &grid, row: row, col: col - 1) 79 } 80 81 //right part 82 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 { 10 for j in 0..<grid[0].count { 11 if grid[i][j] == "1" { 12 queue.append([i, j]) 13 count += 1 14 markIsland(&grid, &queue) 15 } 16 } 17 } 18 return count 19 } 20 21 22 private let dc = [-1, 1, 0, 0] 23 private let dr = [0, 0, 1, -1] 24 25 private func markIsland(_ grid: inout [[Character]], _ queue: inout [[Int]]) { 26 while !queue.isEmpty { 27 let curr = queue.removeFirst() 28 for i in 0..<dc.count { 29 let row = curr[0] + dr[i] 30 let column = curr[1] + dc[i] 31 if row < 0 || row >= grid.count || column < 0 || column >= grid[0].count || grid[row][column] == "0" { 32 continue 33 } 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 return 15 } 16 17 memo[i][j] = true 18 19 for dir in dirs { 20 dfs(i:i + dir[0], j: j + dir[1]) 21 } 22 } 23 24 for i in 0..<grid.count { 25 for j in 0..<grid[i].count { 26 if memo[i][j] == true || grid[i][j] == "0" { 27 continue 28 } else { 29 result += 1 30 dfs(i:i, j:j) 31 } 32 } 33 } 34 35 return result 36 } 37 }
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 = 0 10 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 + 1 26 } 27 } 28 } 29 30 return count 31 } 32 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了