[Swift]LeetCode498. 对角线遍历 | Diagonal Traverse
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10391890.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation:
Note:
The total number of elements of the given matrix will not exceed 10,000.
给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示。
示例:
输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,4,7,5,3,6,8,9] 解释:
说明:
- 给定矩阵中的元素总数不会超过 100000 。
200ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 if matrix.count <= 0{ 4 return [] 5 } 6 7 let m = matrix.count 8 let n = matrix[0].count 9 10 var a: Int = 0 11 var b: Int = 0 12 13 var direct:Int = 0 // 遍历的方向,0表示右上,1表示左下 14 var result: [Int] = [] 15 16 for index in 1...m*n{ 17 if index == 1 { 18 result.append(matrix[b][a]) 19 continue 20 } 21 // 方向右上 22 if direct == 0 { 23 // 横坐标越界,下移 24 if a + 1 >= n{ 25 b += 1 26 direct = 1 27 result.append(matrix[b][a]) 28 continue 29 }else if b - 1 < 0{ 30 // 纵坐标越界,右移动 31 a += 1 32 direct = 1 33 result.append(matrix[b][a]) 34 continue 35 }else{ 36 a += 1 37 b -= 1 38 result.append(matrix[b][a]) 39 continue 40 } 41 } 42 // 方向左下 43 if direct == 1 { 44 // 纵坐标越界,右移动 45 if b + 1 >= m{ 46 a += 1 47 direct = 0 48 result.append(matrix[b][a]) 49 continue 50 }else if a - 1 < 0{ 51 // 横坐标越界,下移 52 b += 1 53 direct = 0 54 result.append(matrix[b][a]) 55 continue 56 }else{ 57 a -= 1 58 b += 1 59 result.append(matrix[b][a]) 60 continue 61 } 62 } 63 } 64 return result 65 } 66 }
216ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 if matrix.count <= 0{ 4 return [] 5 } 6 7 let m = matrix.count 8 let n = matrix[0].count 9 10 var a: Int = 0 11 var b: Int = 0 12 13 var direct:Int = 0 // 遍历的方向,0表示右上,1表示左下 14 var result: [Int] = [] 15 16 for index in 1...m*n{ 17 if index == 1 { 18 result.append(matrix[b][a]) 19 continue 20 } 21 // 方向右上 22 if direct == 0 { 23 // 横坐标越界,下移 24 if a + 1 >= n{ 25 b += 1 26 direct = 1 27 result.append(matrix[b][a]) 28 continue 29 }else if b - 1 < 0{ 30 // 纵坐标越界,右移动 31 a += 1 32 direct = 1 33 result.append(matrix[b][a]) 34 continue 35 }else{ 36 a += 1 37 b -= 1 38 result.append(matrix[b][a]) 39 continue 40 } 41 } 42 // 方向左下 43 if direct == 1 { 44 // 纵坐标越界,右移动 45 if b + 1 >= m{ 46 a += 1 47 direct = 0 48 result.append(matrix[b][a]) 49 continue 50 }else if a - 1 < 0{ 51 // 横坐标越界,下移 52 b += 1 53 direct = 0 54 result.append(matrix[b][a]) 55 continue 56 }else{ 57 a -= 1 58 b += 1 59 result.append(matrix[b][a]) 60 continue 61 } 62 } 63 } 64 return result 65 } 66 }
232ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 4 var res = [Int]() 5 6 let row = matrix.count 7 8 if row == 0 { 9 10 return res 11 } 12 13 let col = matrix[0].count 14 15 16 if col == 0 { 17 18 return res 19 } 20 21 for i in 0 ..< (row - 1 + col) { 22 23 if i < row && i < col { 24 25 for j in 0 ... i { 26 27 if i % 2 == 0 { 28 29 res.append(matrix[i - j][j]) 30 }else { 31 res.append(matrix[j][i - j]) 32 } 33 } 34 35 }else if i < row && i >= col { 36 37 for j in 0 ..< col { 38 39 if i % 2 == 0 { 40 41 res.append(matrix[i - j][j]) 42 }else { 43 res.append(matrix[i - col + 1 + j][col - 1 - j]) 44 } 45 } 46 47 }else if i >= row && i < col { 48 49 for j in 0 ..< row { 50 51 if i % 2 == 0 { 52 53 res.append(matrix[row - 1 - j][i - row + 1 + j]) 54 }else { 55 res.append(matrix[j][i - j]) 56 } 57 } 58 59 }else{ 60 61 for j in 0 ..< row + col - 1 - i { 62 63 if i % 2 == 0 { 64 65 res.append(matrix[row - 1 - j][i - row + 1 + j]) 66 }else { 67 res.append(matrix[i - col + 1 + j][col - 1 - j]) 68 } 69 } 70 71 } 72 73 } 74 75 return res 76 } 77 }
248ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 if matrix.count == 0 { return [] } 4 var result = [Int]() 5 var r = 0, c = 0 6 let m = matrix.count - 1, n = matrix[0].count - 1 7 for _ in 0..<(matrix.count * matrix[0].count) { 8 result.append(matrix[r][c]) 9 if (r + c) % 2 == 0 { 10 if c == n { r += 1; continue } 11 c += 1; 12 r = r > 0 ? r - 1 : r 13 } else { 14 if r == m { c += 1; continue } 15 r += 1; 16 c = c > 0 ? c - 1 : c 17 } 18 } 19 return result 20 } 21 }
252ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 var x = 0; var y = 0; 4 var inc = true 5 var output: [Int] = [] 6 7 if (matrix.count == 0 || matrix[0].count == 0) { return [] } 8 9 while (x < matrix[0].count && y < matrix.count) { 10 output.append(matrix[y][x]) 11 12 if (inc) { 13 if (y-1 >= 0 && x+1 < matrix[0].count) { 14 y -= 1 15 x += 1 16 } 17 else if (x+1 >= matrix[0].count) { 18 inc = false 19 y += 1 20 } 21 else if (y-1 < 0) { 22 inc = false 23 x += 1 24 } 25 } 26 else { 27 if (x-1 >= 0 && y+1 < matrix.count) { 28 x-=1 29 y+=1 30 } 31 else if (y+1 >= matrix.count) { 32 inc = true 33 x += 1 34 } 35 else if(x-1 < 0) { 36 inc = true 37 y += 1 38 } 39 } 40 } 41 42 return output 43 } 44 }
264 ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 if matrix.isEmpty || matrix[0].isEmpty {return []} 4 var m:Int = matrix.count 5 var n:Int = matrix[0].count 6 var k:Int = 0 7 var res:[Int] = [Int](repeating:0,count:m * n) 8 for i in 0..<(m + n - 1) 9 { 10 var low:Int = max(0, i - n + 1) 11 var high = min(i, m - 1) 12 if i % 2 == 0 13 { 14 for j in stride(from:high,through:low,by:-1) 15 { 16 res[k] = matrix[j][i - j] 17 k += 1 18 } 19 } 20 else 21 { 22 for j in low...high 23 { 24 res[k] = matrix[j][i - j] 25 k += 1 26 } 27 } 28 } 29 return res 30 } 31 }
280ms
1 class Solution { 2 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 3 var ans = [Int]() 4 5 let ROWS = matrix.count 6 let COLS = matrix.first?.count ?? 0 7 8 var k = 0 9 var row = 0 10 var col = 0 11 var isUp = true 12 13 while k < ROWS * COLS { 14 15 if isUp { 16 while row >= 0 && col < COLS { 17 ans.append(matrix[row][col]) 18 row -= 1 19 col += 1 20 k += 1 21 } 22 23 if row < 0 && col < COLS { 24 row = 0 25 } 26 27 if col == COLS { 28 row += 2 29 col -= 1 30 } 31 32 } else { 33 while col >= 0 && row < ROWS { 34 ans.append(matrix[row][col]) 35 col -= 1 36 row += 1 37 k += 1 38 } 39 40 if col < 0 && row < ROWS { 41 col = 0 42 } 43 44 if row == ROWS { 45 row -= 1 46 col += 2 47 } 48 } 49 50 isUp = !isUp 51 } 52 53 return ans 54 } 55 }
308ms
1 class Solution { 2 var myX = 0 3 var myY = 0 4 5 func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] { 6 7 if (matrix.count == 0 || matrix[0].count == 0) { 8 return [] 9 } 10 11 var ret = [Int]() 12 13 myX = 0 14 myY = 0 15 16 for i in 0..<matrix.count + matrix[0].count { 17 ret += traverse(matrix, 1, -1) 18 nextRight(matrix[0].count) 19 20 ret += traverse(matrix, -1, 1) 21 nextDown(matrix.count) 22 } 23 24 return ret 25 } 26 27 func nextRight(_ w: Int) { 28 if myX < w - 1 { 29 myX += 1 30 } else { 31 myY += 1 32 } 33 } 34 35 func nextDown(_ h: Int) { 36 if myY < h - 1 { 37 myY += 1 38 } else { 39 myX += 1 40 } 41 } 42 43 func traverse(_ matrix: [[Int]], _ dx: Int, _ dy: Int) -> [Int] { 44 var ret = [Int]() 45 46 while myX >= 0 && myX < matrix[0].count && myY >= 0 && myY < matrix.count { 47 ret.append(matrix[myY][myX]) 48 myX += dx 49 myY += dy 50 } 51 52 myX -= dx 53 myY -= dy 54 55 return ret 56 } 57 }