-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
77 lines (57 loc) · 1.52 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//: [上一道题](@previous)
/*:
# 旋转矩阵
- 题号:[面试题 01.07](https://leetcode-cn.com/problems/rotate-matrix-lcci/)
- 难度:中等
- 描述:
给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。
请你设计一种算法,将图像旋转 90 度。
不占用额外内存空间能否做到?
*/
//: ## Code
import Foundation
func rotate(_ matrix: inout [[Int]]) {
// 占用额外空间
var newMatrix: [[Int]] = []
for _ in matrix {
newMatrix.append([Int](repeating: 0, count: matrix.count))
}
for i in 0 ..< matrix.count {
for j in 0 ..< matrix[i].count {
newMatrix[j][abs(i - matrix.count + 1)] = matrix[i][j]
}
}
matrix = newMatrix
}
func rotate2(_ matrix: inout [[Int]]) {
let n = matrix.count
// 对角线交换
for i in 0 ..< n {
for j in i + 1 ..< matrix[i].count {
(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
}
}
// 每一行以中点进行翻转
for i in 0 ..< n {
for j in 0 ..< matrix[i].count >> 1 {
(matrix[i][j], matrix[i][n - 1 - j]) = (matrix[i][n - 1 - j], matrix[i][j])
}
}
}
//: ## Test
var matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
rotate2(&matrix)
print(matrix)
var matrix2 = [
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
]
rotate2(&matrix2)
print(matrix2)
//: [下一道题](@next)