原文地址:http://www.javashuo.com/article/p-pghwkuuy-cc.html html
Given an m x n
matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.app
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.less
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.spa
Note:code
Example:htm
Given the following 5x5 matrix: Pacific ~ ~ ~ ~ ~ ~ 1 2 2 3 (5) * ~ 3 2 3 (4) (4) * ~ 2 4 (5) 3 1 * ~ (6) (7) 1 4 5 * ~ (5) 1 1 2 4 * * * * * * Atlantic Return: [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
給定一個 m x n
的非負整數矩陣來表示一片大陸上各個單元格的高度。「太平洋」處於大陸的左邊界和上邊界,而「大西洋」處於大陸的右邊界和下邊界。blog
規定水流只能按照上、下、左、右四個方向流動,且只能從高到低或者在同等高度上流動。ci
請找出那些水流既能夠流動到「太平洋」,又能流動到「大西洋」的陸地單元的座標。 get
提示:it
示例:
給定下面的 5x5 矩陣: 太平洋 ~ ~ ~ ~ ~ ~ 1 2 2 3 (5) * ~ 3 2 3 (4) (4) * ~ 2 4 (5) 3 1 * ~ (6) (7) 1 4 5 * ~ (5) 1 1 2 4 * * * * * * 大西洋 返回: [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (上圖中帶括號的單元).
320ms
1 class Solution { 2 func pacificAtlantic(_ matrix: [[Int]]) -> [[Int]] { 3 var matrix = matrix 4 if matrix.isEmpty || matrix[0].isEmpty {return []} 5 var res:[[Int]] = [[Int]]() 6 var m:Int = matrix.count 7 var n:Int = matrix[0].count 8 var pacific:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 9 var atlantic:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 10 for i in 0..<m 11 { 12 dfs(&matrix, &pacific, Int.min, i, 0) 13 dfs(&matrix, &atlantic, Int.min, i, n - 1) 14 } 15 for i in 0..<n 16 { 17 dfs(&matrix, &pacific, Int.min, 0, i) 18 dfs(&matrix, &atlantic, Int.min, m - 1, i) 19 } 20 21 for i in 0..<m 22 { 23 for j in 0..<n 24 { 25 if pacific[i][j] && atlantic[i][j] 26 { 27 res.append([i,j]) 28 } 29 } 30 } 31 return res 32 } 33 34 func dfs(_ matrix:inout [[Int]],_ visited:inout [[Bool]],_ pre:Int,_ i:Int,_ j:Int) 35 { 36 var m:Int = matrix.count 37 var n:Int = matrix[0].count 38 if i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || matrix[i][j] < pre 39 { 40 return 41 } 42 visited[i][j] = true 43 dfs(&matrix, &visited, matrix[i][j], i + 1, j) 44 dfs(&matrix, &visited, matrix[i][j], i - 1, j) 45 dfs(&matrix, &visited, matrix[i][j], i, j + 1) 46 dfs(&matrix, &visited, matrix[i][j], i, j - 1) 47 } 48 }
3572ms
1 class Solution { 2 var reachable: [[(Bool, Bool)]] = [] 3 4 func pacificAtlantic(_ matrix: [[Int]]) -> [[Int]] { 5 guard !matrix.isEmpty && !matrix[0].isEmpty else { return [] } 6 let n = matrix.count 7 let m = matrix[0].count 8 reachable = Array(repeating: Array(repeating: (false, false), count: m), count: n) 9 var visited: [[Bool]] = Array(repeating: Array(repeating: false, count: m), count: n) 10 11 for i in 0 ..< n { 12 for j in 0 ..< m { 13 dfs(matrix, i, j, &visited) 14 } 15 } 16 17 var res: [[Int]] = [] 18 19 for i in 0 ..< n { 20 for j in 0 ..< m { 21 if reachable[i][j].0 && reachable[i][j].1 { 22 res.append([i, j]) 23 } 24 } 25 } 26 return res 27 } 28 29 private func dfs(_ matrix: [[Int]], _ x: Int, _ y: Int, _ visited: inout [[Bool]]) -> (Bool, Bool) { 30 let n = matrix.count 31 let m = matrix[0].count 32 if reachable[x][y].0 && reachable[x][y].1 { 33 return (true, true) 34 } 35 visited[x][y] = true 36 let dirs: [(Int, Int)] = [(-1, 0), (0, -1), (1, 0), (0, 1)] 37 for dir in dirs { 38 let newX = x + dir.0 39 let newY = y + dir.1 40 if newX >= 0 && newX < n && newY >= 0 && newY < m && matrix[newX][newY] > matrix[x][y] { 41 reachable[x][y].0 = reachable[x][y].0 || false 42 reachable[x][y].1 = reachable[x][y].1 || false 43 } else if newX < 0 || newX >= n || newY < 0 || newY >= m { 44 reachable[x][y].0 = reachable[x][y].0 || (newX < 0 || newY < 0) 45 reachable[x][y].1 = reachable[x][y].1 || (newX >= n || newY >= m) 46 } else if !visited[newX][newY] { 47 let res = dfs(matrix, newX, newY, &visited) 48 reachable[x][y].0 = reachable[x][y].0 || res.0 49 reachable[x][y].1 = reachable[x][y].1 || res.1 50 } 51 } 52 visited[x][y] = false 53 return reachable[x][y] 54 } 55 }