File tree 1 file changed +6
-10
lines changed
1 file changed +6
-10
lines changed Original file line number Diff line number Diff line change 6
6
*/
7
7
8
8
class SpiralMatrixII {
9
- func generateMatrix( n: Int ) -> [ [ Int ] ] {
9
+ func generateMatrix( _ n: Int ) -> [ [ Int ] ] {
10
10
guard n > 0 else {
11
11
return [ [ Int] ] ( )
12
12
}
13
13
14
14
var num = 1
15
- var res = Array ( count: n, repeatedValue: Array ( count: n, repeatedValue: 0 ) )
16
-
17
- var start = 0
18
- var end = 0
19
- var offset = 0
15
+ var res = Array ( repeating: Array ( repeating: 0 , count: n) , count: n)
20
16
21
17
for layer in 0 ..< n / 2 {
22
- start = layer
23
- end = n - layer - 1
18
+ let start = layer
19
+ let end = n - layer - 1
24
20
25
21
// top
26
22
for i in start ..< end {
@@ -35,13 +31,13 @@ class SpiralMatrixII {
35
31
}
36
32
37
33
// bottom
38
- for i in end . stride ( to: start, by: - 1 ) {
34
+ for i in stride ( from : end , to: start, by: - 1 ) {
39
35
res [ end] [ i] = num
40
36
num += 1
41
37
}
42
38
43
39
// left
44
- for i in end . stride ( to: start, by: - 1 ) {
40
+ for i in stride ( from : end , to: start, by: - 1 ) {
45
41
res [ i] [ start] = num
46
42
num += 1
47
43
}
You can’t perform that action at this time.
0 commit comments