Skip to content

Commit 991cb6e

Browse files
authored
Merge pull request halfrost#133 from NovaHe/fix/48
fix/48: clean up code
2 parents 4330393 + bb50f33 commit 991cb6e

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
package leetcode
22

33
func rotate(matrix [][]int) {
4-
row := len(matrix)
5-
if row <= 0 {
6-
return
7-
}
8-
column := len(matrix[0])
4+
length := len(matrix)
95
// rotate by diagonal 对角线变换
10-
for i := 0; i < row; i++ {
11-
for j := i + 1; j < column; j++ {
12-
tmp := matrix[i][j]
13-
matrix[i][j] = matrix[j][i]
14-
matrix[j][i] = tmp
6+
for i := 0; i < length; i++ {
7+
for j := i + 1; j < length; j++ {
8+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
159
}
1610
}
1711
// rotate by vertical centerline 竖直轴对称翻转
18-
halfColumn := column / 2
19-
for i := 0; i < row; i++ {
20-
for j := 0; j < halfColumn; j++ {
21-
tmp := matrix[i][j]
22-
matrix[i][j] = matrix[i][column-j-1]
23-
matrix[i][column-j-1] = tmp
12+
for i := 0; i < length; i++ {
13+
for j := 0; j < length/2; j++ {
14+
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
2415
}
2516
}
2617
}

website/content/ChapterFour/0001~0099/0048.Rotate-Image.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,35 +97,23 @@ You have to rotate the image **[in-place](https://en.wikipedia.org/wiki/In-plac
9797
## 代码
9898
9999
```go
100-
101100
package leetcode
102101
103102
func rotate(matrix [][]int) {
104-
row := len(matrix)
105-
if row <= 0 {
106-
return
107-
}
108-
column := len(matrix[0])
103+
length := len(matrix)
109104
// rotate by diagonal 对角线变换
110-
for i := 0; i < row; i++ {
111-
for j := i + 1; j < column; j++ {
112-
tmp := matrix[i][j]
113-
matrix[i][j] = matrix[j][i]
114-
matrix[j][i] = tmp
105+
for i := 0; i < length; i++ {
106+
for j := i + 1; j < length; j++ {
107+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
115108
}
116109
}
117110
// rotate by vertical centerline 竖直轴对称翻转
118-
halfColumn := column / 2
119-
for i := 0; i < row; i++ {
120-
for j := 0; j < halfColumn; j++ {
121-
tmp := matrix[i][j]
122-
matrix[i][j] = matrix[i][column-j-1]
123-
matrix[i][column-j-1] = tmp
111+
for i := 0; i < length; i++ {
112+
for j := 0; j < length/2; j++ {
113+
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
124114
}
125115
}
126116
}
127-
128-
129117
```
130118

131119

0 commit comments

Comments
 (0)