★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9917757.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
8ms
1 class Solution { 2 func spiralOrder(_ matrix: [[Int]]) -> [Int] { 3 var res = [Int]() 4 if (matrix.count == 0) { 5 return res 6 } 7 var startX = 0 8 var endX = matrix.count - 1 9 var startY = 0 10 var endY = matrix[0].count - 1 11 12 while(true) { 13 for i in startY...endY { 14 res.append(matrix[startX][i]) 15 } 16 startX += 1 17 if (startX > endX) { 18 break 19 } 20 21 for i in startX...endX { 22 res.append(matrix[i][endY]) 23 } 24 endY -= 1 25 if (endY < startY) { 26 break 27 } 28 29 30 for i in stride(from: endY, through:startY, by:-1) { 31 res.append(matrix[endX][i]) 32 } 33 endX -= 1 34 if (endX < startX) { 35 break 36 } 37 for i in stride(from:endX, through:startX, by:-1) { 38 res.append(matrix[i][startY]) 39 } 40 startY += 1 41 if (startY > endY) { 42 break 43 } 44 } 45 return res 46 } 47 }
8ms
1 class Solution { 2 func spiralOrder(_ matrix: [[Int]]) -> [Int] { 3 guard !matrix.isEmpty else { 4 return [] 5 } 6 7 var column = 0 8 var row = 0 9 10 let loopCount = (min(matrix.count, matrix[0].count) + 1) / 2 11 12 var numbers: [Int] = [] 13 14 for loopIndex in 0..<loopCount { 15 16 let lastColumn = matrix[0].count - loopIndex - 1 17 let lastRow = matrix.count - loopIndex - 1 18 19 if loopIndex == lastRow { 20 for index in loopIndex...lastColumn { 21 numbers.append(matrix[loopIndex][index]) 22 } 23 } else if loopIndex == lastColumn { 24 for index in loopIndex...lastRow { 25 numbers.append(matrix[index][loopIndex]) 26 } 27 } else { 28 for index in loopIndex..<lastColumn { 29 numbers.append(matrix[loopIndex][index]) 30 } 31 32 for index in loopIndex..<lastRow { 33 numbers.append(matrix[index][lastColumn]) 34 } 35 36 var index = lastColumn 37 while index > loopIndex { 38 numbers.append(matrix[lastRow][index]) 39 index -= 1 40 } 41 42 index = lastRow 43 while index > loopIndex { 44 numbers.append(matrix[index][loopIndex]) 45 index -= 1 46 } 47 } 48 } 49 50 return numbers 51 } 52 }
12ms
1 class Solution { 2 func spiralOrder(_ matrix: [[Int]]) -> [Int] { 3 if(matrix == nil || matrix.count == 0) { 4 return []; 5 } 6 var rows = matrix.count; 7 var cols = matrix[0].count; 8 var col = 0 9 var row = 0 10 print(cols) 11 var outputArray = [Int]() 12 while(row < rows && col < cols) { 13 for i in col...cols-1{ 14 print("inside1") 15 print(row,i) 16 outputArray.append(matrix[row][i]) 17 } 18 row += 1 19 if(row <= rows-1) { 20 for i in row...rows-1{ 21 print("inside2") 22 print(i,cols-1) 23 outputArray.append(matrix[i][cols-1]) 24 } 25 } 26 cols -= 1 27 //print left 28 if(row <= rows-1 && col <= cols-1) { 29 for i in (col...cols-1).reversed() { 30 print("inside3") 31 print(rows-1,i) 32 outputArray.append(matrix[(rows-1)][i]) 33 } 34 rows -= 1 35 } 36 //print up 37 if(col <= cols-1 && row <= rows-1) { 38 for i in (row...rows-1).reversed() { 39 print("inside4") 40 print(i,col) 41 outputArray.append(matrix[i][col]) 42 } 43 col += 1 44 } 45 } 46 return outputArray 47 } 48 }
12ms
1 class Solution { 2 func spiralOrder(_ matrix: [[Int]]) -> [Int] { 3 4 if matrix.count < 1 { 5 return [] 6 } 7 8 let m = matrix.count 9 let n = matrix[0].count 10 11 var result: [Int] = [] 12 13 var a: Int = 0 14 var b: Int = 0 15 16 var direct: Int = 0 // 0表示向右,1表示向下,2表示向左,3表示向右 17 18 // 定义边界 19 var top: Int = -1 20 var left: Int = -1 21 var right: Int = n 22 var bottom: Int = m 23 24 for index in 1...m*n { 25 26 if index == 1 { 27 result.append(matrix[b][a]) 28 continue 29 } 30 if direct == 0{ 31 if a+1 >= right && b+1 < bottom { 32 b += 1 33 top += 1 34 direct = 1 35 }else if a+1 >= right && b+1 >= bottom{ 36 break 37 }else{ 38 a += 1 39 } 40 result.append(matrix[b][a]) 41 continue 42 43 }else if direct == 1{ 44 if b+1 >= bottom && a-1 > left{ 45 // 下边即将越界,转移方向向左便利 46 a -= 1 47 right -= 1 // 一行遍历完了 48 direct = 2 49 }else if b+1 >= bottom && a-1 <= left{ 50 break 51 }else{ 52 b += 1 53 } 54 result.append(matrix[b][a]) 55 continue 56 }else if direct == 2{ 57 if a-1 <= left && b-1 > top{ 58 // 右边即将越界,转移方向向下便利 59 b -= 1 60 bottom -= 1 // 一行遍历完了 61 direct = 3 62 }else if a-1 <= left && b-1 <= top{ 63 break 64 }else{ 65 a -= 1 66 } 67 result.append(matrix[b][a]) 68 continue 69 }else if direct == 3{ 70 if b-1 <= top && a+1 < right{ 71 // 右边即将越界,转移方向向下便利 72 a += 1 73 left += 1 // 一行遍历完了 74 direct = 0 75 }else if b-1 <= top && a+1 >= right{ 76 break 77 }else{ 78 b -= 1 79 } 80 result.append(matrix[b][a]) 81 continue 82 } 83 } 84 85 return result 86 } 87 }
20ms
1 class Solution { 2 func spiralOrder(_ matrix: [[Int]]) -> [Int] { 3 4 5 var result: [Int] = [] 6 if let firstCol = matrix.first { 7 var x = 0 8 var y = 0 9 var row = matrix.count - 1 10 var col = firstCol.count - 1 11 while x <= row && y <= col { 12 13 if x <= col { 14 for i in x ... col { 15 result.append(matrix[y][i]) 16 } 17 } 18 19 if y + 1 <= row { 20 for j in y + 1 ... row { 21 result.append(matrix[j][col]) 22 } 23 } 24 25 if x <= col - 1 && y != row { 26 for i in (x ... col - 1).reversed() { 27 result.append(matrix[row][i]) 28 } 29 } 30 31 if y < row - 1 && x != col { 32 for j in (y + 1 ... row - 1).reversed() { 33 result.append(matrix[j][x]) 34 } 35 } 36 37 x += 1 38 y += 1 39 row -= 1 40 col -= 1 41 42 } 43 } 44 45 return result 46 } 47 }
24ms
1 class Solution { 2 func spiralOrder(_ 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 var top = 0, right = col - 1, bottom = row - 1, left = 0 22 23 var direction = 0 24 25 while top <= bottom && left <= right { 26 27 if direction % 4 == 0 { 28 29 for i in left ... right { 30 31 res.append(matrix[top][i]) 32 } 33 34 top += 1 35 } 36 37 if direction % 4 == 1 { 38 39 for i in top ... bottom { 40 41 res.append(matrix[i][right]) 42 } 43 44 right -= 1 45 } 46 47 if direction % 4 == 2 { 48 49 for i in stride(from: right, through: left, by: -1) { 50 51 res.append(matrix[bottom][i]) 52 } 53 54 bottom -= 1 55 } 56 57 if direction % 4 == 3 { 58 59 for i in stride(from: bottom, through: top, by: -1){ 60 61 res.append(matrix[i][left]) 62 } 63 64 left += 1 65 } 66 67 direction += 1 68 } 69 70 return res 71 } 72 }