mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-12 06:36:42 +08:00
fix/48: clean up code
This commit is contained in:
@ -1,26 +1,17 @@
|
|||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
func rotate(matrix [][]int) {
|
func rotate(matrix [][]int) {
|
||||||
row := len(matrix)
|
length := len(matrix)
|
||||||
if row <= 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
column := len(matrix[0])
|
|
||||||
// rotate by diagonal 对角线变换
|
// rotate by diagonal 对角线变换
|
||||||
for i := 0; i < row; i++ {
|
for i := 0; i < length; i++ {
|
||||||
for j := i + 1; j < column; j++ {
|
for j := i + 1; j < length; j++ {
|
||||||
tmp := matrix[i][j]
|
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
|
||||||
matrix[i][j] = matrix[j][i]
|
|
||||||
matrix[j][i] = tmp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// rotate by vertical centerline 竖直轴对称翻转
|
// rotate by vertical centerline 竖直轴对称翻转
|
||||||
halfColumn := column / 2
|
for i := 0; i < length; i++ {
|
||||||
for i := 0; i < row; i++ {
|
for j := 0; j < length/2; j++ {
|
||||||
for j := 0; j < halfColumn; j++ {
|
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
|
||||||
tmp := matrix[i][j]
|
|
||||||
matrix[i][j] = matrix[i][column-j-1]
|
|
||||||
matrix[i][column-j-1] = tmp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,35 +97,23 @@ You have to rotate the image **[in-place](https://en.wikipedia.org/wiki/In-plac
|
|||||||
## 代码
|
## 代码
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
func rotate(matrix [][]int) {
|
func rotate(matrix [][]int) {
|
||||||
row := len(matrix)
|
length := len(matrix)
|
||||||
if row <= 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
column := len(matrix[0])
|
|
||||||
// rotate by diagonal 对角线变换
|
// rotate by diagonal 对角线变换
|
||||||
for i := 0; i < row; i++ {
|
for i := 0; i < length; i++ {
|
||||||
for j := i + 1; j < column; j++ {
|
for j := i + 1; j < length; j++ {
|
||||||
tmp := matrix[i][j]
|
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
|
||||||
matrix[i][j] = matrix[j][i]
|
|
||||||
matrix[j][i] = tmp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// rotate by vertical centerline 竖直轴对称翻转
|
// rotate by vertical centerline 竖直轴对称翻转
|
||||||
halfColumn := column / 2
|
for i := 0; i < length; i++ {
|
||||||
for i := 0; i < row; i++ {
|
for j := 0; j < length/2; j++ {
|
||||||
for j := 0; j < halfColumn; j++ {
|
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
|
||||||
tmp := matrix[i][j]
|
|
||||||
matrix[i][j] = matrix[i][column-j-1]
|
|
||||||
matrix[i][column-j-1] = tmp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user