• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode200.岛屿的个数|NumberofIslands

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10180994.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ios 国际化使用多国语言(Swift)发布时间:2022-07-13
下一篇:
Swift中的String和NSString 以及其编码方式的坑发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap