[Swift]LeetCode54. 螺旋矩阵 | Spiral Matrix

[Swift]LeetCode54. 螺旋矩阵 | Spiral Matrix,第1张

概述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

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,3,6,9,7,4,5]

Example 2:

input:[  [1,4],[5,8],[9,10,11,12]]Output: [1,12,7]

给定一个包含 m x n 个元素的矩阵m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:[ [ 1,9 ]]输出: [1,5]

示例 2:

输入:[  [1,12]]输出: [1,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 = 010         var endY = matrix[0].count - 111         12         while(true) {13             for i in startY...endY {14                 res.append(matrix[startX][i])15             }16             startX += 117             if (startX > endX) {18                 break19             }20             21             for i in startX...endX {22                 res.append(matrix[i][endY])23             }24             endY -= 125             if (endY < startY) {26                 break27             }28 29 30             for i in strIDe(from: endY,through:startY,by:-1) {31                 res.append(matrix[endX][i])32             }33             endX -= 134             if (endX < startX) {35                 break36             }37             for i in strIDe(from:endX,through:startX,by:-1) {38                 res.append(matrix[i][startY])39             }40             startY += 141             if (startY > endY) {42                 break43             }44         }45         return res46     }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) / 211 12         var numbers: [Int] = []13 14         for loopIndex in 0..<loopCount {15 16             let lastColumn = matrix[0].count - loopIndex - 117             let lastRow = matrix.count - loopIndex - 118 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 = lastColumn37                 while index > loopIndex {38                     numbers.append(matrix[lastRow][index])39                     index -= 140                 }41 42                 index = lastRow43                 while index > loopIndex {44                     numbers.append(matrix[index][loopIndex])45                     index -= 146                 }47             }48         }49 50         return numbers51     }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 = 010         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 += 119             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 -= 127             //print left28             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 -= 135             }36             //print up37             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 += 144             }45         }46         return outputArray47     }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].count10         11         var result: [Int] = []12         13         var a: Int = 014         var b: Int = 015         16         var direct: Int = 0 // 0表示向右,1表示向下,2表示向左,3表示向右17         18         // 定义边界19         var top: Int = -120         var left: Int = -121         var right: Int = n22         var bottom: Int = m23         24         for index in 1...m*n {25          26             if index == 1 {27                 result.append(matrix[b][a])28                 continue29             }30             if direct == 0{31                 if a+1 >= right && b+1 < bottom {32                     b += 133                     top += 134                     direct = 135                 }else if a+1 >= right && b+1 >= bottom{36                     break37                 }else{38                     a += 139                 }40                 result.append(matrix[b][a])41                 continue42                 43             }else if direct == 1{44                 if b+1 >= bottom && a-1 > left{45                     // 下边即将越界,转移方向向左便利46                     a -= 147                     right -= 1 // 一行遍历完了48                     direct = 249                 }else if b+1 >= bottom && a-1 <= left{50                     break51                 }else{52                     b += 153                 }54                 result.append(matrix[b][a])55                 continue56             }else if direct == 2{57                 if a-1 <= left && b-1 > top{58                     // 右边即将越界,转移方向向下便利59                     b -= 160                     bottom -= 1 // 一行遍历完了61                     direct = 362                 }else if a-1 <= left && b-1 <= top{63                     break64                 }else{65                     a -= 166                 }67                 result.append(matrix[b][a])68                 continue69             }else if direct == 3{70                 if b-1 <= top && a+1 < right{71                     // 右边即将越界,转移方向向下便利72                     a += 173                     left += 1 // 一行遍历完了74                     direct = 075                 }else if b-1 <= top && a+1 >= right{76                     break77                 }else{78                     b -= 179                 }80                 result.append(matrix[b][a])81                 continue82             }83         }84         85         return result86     }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 - 110             var col = firstCol.count - 111             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 += 138                 y += 139                 row -= 140                 col -= 141                 42             }43         }44         45         return result46     }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 res11         }12         13         let col = matrix[0].count14         15         16         if col == 0 {17             18             return res19         }20         21         var top = 0,right = col - 1,bottom = row - 1,left = 022         23         var direction = 024         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 += 135             }36             37             if direction % 4 == 1 {38                 39                 for i in top ... bottom {40                     41                     res.append(matrix[i][right])42                 }43                 44                 right -= 145             }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 -= 155             }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 += 165             }66             67             direction += 168         }69         70         return res71     }72 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode54. 螺旋矩阵 | Spiral Matrix全部内容,希望文章能够帮你解决[Swift]LeetCode54. 螺旋矩阵 | Spiral Matrix所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1023063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存