为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode59. 螺旋矩阵 II | Spiral Matrix II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9920694.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Example:

Input: 3
Output:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

8ms
 1 class Solution {
 2     func generateMatrix(_ n: Int) -> [[Int]] {
 3         if n == 0 {
 4             return [[Int]]();
 5         }
 6         var i = 0, j = 0;
 7         var result = [[Int]]()
 8         let m = n
 9         var minI = -1, minJ = -1, maxI = m, maxJ = n;
10 
11         let maxLength = m * n
12         
13         var tempArray = [Int]()
14         for i in 0 ..< maxLength {
15             if tempArray.count == n {
16                 result.append(tempArray)
17                 tempArray.removeAll()
18                 tempArray.append(i)
19             } else {
20                 tempArray.append(i)
21             }
22         }
23         result.append(tempArray)
24 
25         //action: 0: i+, 1: j+, 2: i-, 3: j-
26         var action = 1
27         var value = 1
28        
29 
30         while value <= maxLength {
31             switch action {
32             case 0: do {
33                 while i < maxI {
34                     result[i][j] = value
35                     value += 1
36                     i += 1
37                 }
38                 i -= 1
39                 maxJ -= 1
40 
41                 action = 3
42                 j -= 1
43             }
44             break;
45             case 1: do {
46                 while j < maxJ {
47                     result[i][j] = value
48                     value += 1
49                     j += 1
50                 }
51                 j -= 1
52                 minI += 1
53 
54                 action = 0
55                 i += 1
56             }
57             break;
58             case 2: do {
59                 while i > minI {
60                     result[i][j] = value
61                     value += 1
62                     i -= 1
63                 }
64                 i += 1
65                 minJ += 1
66 
67                 action = 1
68                 j += 1
69             }
70             break;
71             case 3: do {
72                 while j > minJ {
73                     result[i][j] = value
74                     value += 1
75                     j -= 1
76                 }
77                 j += 1
78                 maxI -= 1
79 
80                 action = 2
81                 i -= 1
82             }
83             break;
84             default:
85                 break
86             }
87         }
88         return result;
89     }
90 }

12ms

 1 class Solution {
 2     func generateMatrix(_ n: Int) -> [[Int]] {
 3     let iterations = n / 2 + n % 2
 4         var arr = Array(repeating: (Array(repeating: 0, count: n)), count: n)
 5         var value = 0
 6         var count = n 
 7         var isIterating = false
 8         for index in 0..<iterations {
 9             var j = index
10             var i = index
11             
12             
13             
14             while (j < count) {
15                 value += 1
16                 arr[i][j] = value
17                 j += 1
18                 isIterating = true
19             }
20             j -= 1
21             
22             i += 1
23             while (i < count) {
24                 value += 1
25                 arr[i][j] = value
26                 i += 1
27                 isIterating = true
28             }
29             i -= 1
30             
31             j -= 1
32             
33             if !isIterating  { break }
34             
35             while(j >= index) {
36                 value += 1
37                 arr[i][j] = value
38                 j -= 1
39             }
40             j += 1
41             
42             i -= 1
43             
44             while(i > index) {
45                 value += 1
46                 arr[i][j] = value
47                 i -= 1
48             }
49             
50             count -= 1
51             
52             isIterating = false
53         }
54         
55         return arr
56     }
57 }

12ms

 1 class Solution {
 2         func generateMatrix(_ n: Int) -> [[Int]] {
 3         guard n > 0 else {
 4             return [[Int]]()
 5         }
 6     
 7         var num = 1
 8         var res = Array(repeating: Array(repeating: 0, count: n), count: n)
 9         
10         for layer in 0..<n / 2 {
11             let start = layer
12             let end = n - layer - 1
13             
14             // top
15             for i in start..<end {
16                 res[start][i] = num
17                 num += 1
18             }
19             
20             // right
21             for i in start..<end {
22                 res[i][end] = num
23                 num += 1
24             }
25             
26             // bottom
27             for i in stride(from: end, to: start, by: -1) {
28                 res[end][i] = num
29                 num += 1
30             }
31             
32             // left
33             for i in stride(from: end, to: start, by: -1) {
34                 res[i][start] = num
35                 num += 1
36             }
37         }
38         
39         // handle the center one
40         if n % 2 != 0 {
41             res[n / 2][n / 2] = n * n
42         }
43         
44         return res
45     }
46 }

16ms

 1 class Solution {
 2 func generateMatrix(_ n: Int) -> [[Int]] {
 3         var n = n
 4         var result = Array<[Int]>(repeating: Array<Int>(repeating: 0, count: n), count: n)
 5         var i = 1
 6         let max = n * n
 7         var x = 0, y = 0
 8         while i <= max {
 9             while y < n && result[x][y] == 0 {  //向右
10                 result[x][y] = i
11                 i += 1
12                 y += 1
13             }
14             x += 1
15             y -= 1
16             while x < n && result[x][y] == 0 {  //向下
17                 result[x][y] = i
18                 i += 1
19                 x += 1
20             }
21             x -= 1
22             y -= 1
23             while y >= 0 && result[x][y] == 0 { //向左
24                 result[x][y] = i
25                 i += 1
26                 y -= 1
27             }
28             y += 1
29             x -= 1
30             while x >= 0 && result[x][y] == 0 { //向上
31                 result[x][y] = i
32                 i += 1
33                 x -= 1
34             }
35             x += 1
36             y += 1
37             n -= 1
38         }
39         return result
40     }
41 }

16ms

 1 class Solution {
 2     func generateMatrix(_ n: Int) -> [[Int]] {
 3         
 4         var minRow = 0, maxRow = n - 1, minCol = 0, maxCol = n - 1
 5         var result = [[Int]](repeating: [Int](repeating: 0, count: n), count: n)
 6         
 7         var num = 0
 8         while true {
 9             for j in minCol...maxCol {
10                 num += 1
11                 result[minRow][j] = num
12             }
13             minRow += 1
14             if num == n * n { break }
15             
16             for i in minRow...maxRow {
17                 num += 1
18                 result[i][maxCol] = num
19             }
20             maxCol -= 1
21             if num == n * n { break }
22             
23             for j in (minCol...maxCol).reversed() {
24                 num += 1
25                 result[maxRow][j] = num
26             }
27             maxRow -= 1
28             if num == n * n { break }
29             
30             for i in (minRow...maxRow).reversed() {
31                 num += 1
32                 result[i][minCol] = num
33             }
34             minCol += 1
35             if num == n * n { break }
36         }
37         
38         return result
39         
40     }
41 }

20ms

 1 class Solution {
 2     func generateMatrix(_ n: Int) -> [[Int]] {
 3         var res = [[Int]](repeating: [Int](repeating: 0, count: n), count: n)
 4         var k = 1
 5         for i in 0..<(n/2){//圈数
 6             for y in i..<(n-i){
 7                 res[i][y] = k
 8                 k += 1
 9             }
10             for x in (i+1)..<(n-1-i){
11                 res[x][n-1-i] = k
12                 k += 1
13             }
14             for y in i..<(n-i){
15                 res[n-1-i][n-1-y] = k
16                 k += 1
17             }
18             for x in (i+1)..<(n-1-i){
19                 res[n-1-x][i] = k
20                 k += 1
21             }
22         }
23         if n % 2 != 0{
24             res[n/2][n/2] = k
25         }
26         return res
27     }
28 }

 

posted @ 2018-11-07 09:37  为敢技术  阅读(320)  评论(0编辑  收藏  举报