[Swift]LeetCode200.島嶼的個數 | Number of Islands
阿新 • • 發佈:2018-12-26
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 }