Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50aa41b

Browse files
committed
commit solution 48
1 parent 4b3fb37 commit 50aa41b

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

‎solution/1-99/0048.rotate-image/README.md‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [48. 旋转图像](https://leetcode-cn.com/problems/rotate-image)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个 <em>n&nbsp;</em>&times;&nbsp;<em>n</em> 的二维矩阵表示一个图像。</p>
66

77
<p>将图像顺时针旋转 90 度。</p>
@@ -50,14 +50,27 @@
5050

5151
### 解题思路
5252

53+
![](http://lc-photo.xwlin.com/48-1.png)
54+
![](http://lc-photo.xwlin.com/48-2.png)
55+
![](http://lc-photo.xwlin.com/48-3.png)
56+
![](http://lc-photo.xwlin.com/48-4.png)
57+
![](http://lc-photo.xwlin.com/48-5.png)
5358

5459
### 具体解法
5560

56-
<!-- tabs:start -->
5761

5862
#### **Golang**
5963
```go
60-
64+
func rotate(matrix [][]int) {
65+
size := len(matrix)
66+
subsize := size - 1
67+
for i := 0; i < size/2; i++ {
68+
for j := i; j < subsize-i; j++ {
69+
matrix[i][j], matrix[j][subsize-i] = matrix[j][subsize-i], matrix[i][j]
70+
matrix[i][j], matrix[subsize-i][subsize-j] = matrix[subsize-i][subsize-j], matrix[i][j]
71+
matrix[i][j], matrix[subsize-j][i] = matrix[subsize-j][i], matrix[i][j]
72+
}
73+
}
74+
}
6175
```
6276

63-
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=48 lang=golang
5+
*
6+
* [48] 旋转图像
7+
*/
8+
9+
// @lc code=start
10+
func rotate(matrix [][]int) {
11+
size := len(matrix)
12+
subsize := size - 1
13+
for i := 0; i < size/2; i++ {
14+
for j := i; j < subsize-i; j++ {
15+
matrix[i][j], matrix[j][subsize-i] = matrix[j][subsize-i], matrix[i][j]
16+
matrix[i][j], matrix[subsize-i][subsize-j] = matrix[subsize-i][subsize-j], matrix[i][j]
17+
matrix[i][j], matrix[subsize-j][i] = matrix[subsize-j][i], matrix[i][j]
18+
}
19+
}
20+
}
21+
22+
// @lc code=end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRotate(t *testing.T) {
8+
var matrix [][]int
9+
10+
matrix = [][]int{
11+
{1, 2, 3},
12+
{4, 5, 6},
13+
{7, 8, 9},
14+
}
15+
rotate(matrix)
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /