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

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 }

原文地址:https://www.cnblogs.com/strengthen/p/9920694.html

时间: 2024-07-31 14:30:00

[Swift]LeetCode59. 螺旋矩阵 II | Spiral Matrix II的相关文章

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

Leetcode 细节实现题 Spiral Matrix II

Spiral Matrix II Total Accepted: 12773 Total Submissions: 41526My Submissions Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2,

LeetCode: Spiral Matrix II [058]

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [题意] 给定整数n, 将1,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

Spiral Matrix II

Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 这道题接着上面那道题,是一个逆过程,只要稍微改一下就好了,因

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 解题思路: 求一个

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似