在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower. Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean. Note:
Example: 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). 给定一个 规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。 提示:
示例: 给定下面的 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 }
|
请发表评论