Skip to content

Commit df22052

Browse files
committed
add 48
1 parent 55a826f commit df22052

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
| 283 | [Move Zeroes][283] |
2626
| 1 | [Two Sum][001] |
2727
| 36 | [Valid Sudoku][036] |
28+
| 48 | [Rotate Image][048] |
29+
2830

2931
**其他**
3032

@@ -49,3 +51,4 @@
4951
[283]: https://github.com/andavid/leetcode-java/blob/master/note/283/README.md
5052
[001]: https://github.com/andavid/leetcode-java/blob/master/note/001/README.md
5153
[036]: https://github.com/andavid/leetcode-java/blob/master/note/036/README.md
54+
[048]: https://github.com/andavid/leetcode-java/blob/master/note/048/README.md

note/048/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [Rotate Image][title]
2+
3+
## Description
4+
5+
You are given an n x n 2D matrix representing an image.
6+
7+
Rotate the image by 90 degrees (clockwise).
8+
9+
**Note:**
10+
You have to rotate the image **in-place**, which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotation.
11+
12+
**Example 1:**
13+
14+
```
15+
Given input matrix =
16+
[
17+
[1,2,3],
18+
[4,5,6],
19+
[7,8,9]
20+
],
21+
22+
rotate the input matrix in-place such that it becomes:
23+
[
24+
[7,4,1],
25+
[8,5,2],
26+
[9,6,3]
27+
]
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Given input matrix =
34+
[
35+
[ 5, 1, 9,11],
36+
[ 2, 4, 8,10],
37+
[13, 3, 6, 7],
38+
[15,14,12,16]
39+
],
40+
41+
rotate the input matrix in-place such that it becomes:
42+
[
43+
[15,13, 2, 5],
44+
[14, 3, 4, 1],
45+
[12, 6, 8, 9],
46+
[16, 7,10,11]
47+
]
48+
```
49+
50+
## 思路
51+
52+
题意是给一个 n x n 的二维数组,将数组元素顺时针旋转 90 度。需要直接修改当前数组。
53+
54+
思路是从外往内,一圈一圈的进行。其中每一圈的每个元素顺时针旋转 90 度,连续操作四次。注意边界,防止重复操作。
55+
56+
## [完整代码][src]
57+
58+
```java
59+
class Solution {
60+
public void rotate(int[][] matrix) {
61+
int n = matrix.length;
62+
for (int i = 0; i < n / 2; i ++) {
63+
for (int j = i; j < n - 1 - i; j ++) {
64+
int temp = matrix[i][j];
65+
matrix[i][j] = matrix[n - 1 -j][i];
66+
matrix[n - 1 -j][i] = matrix[n - 1 -i][n - 1 -j];
67+
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 -i];
68+
matrix[j][n - 1 -i] = temp;
69+
}
70+
}
71+
}
72+
}
73+
```
74+
75+
[title]: https://leetcode.com/problems/rotate-image
76+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_048/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public void rotate(int[][] matrix) {
3+
int n = matrix.length;
4+
for (int i = 0; i < n / 2; i ++) {
5+
for (int j = i; j < n - 1 - i; j ++) {
6+
int temp = matrix[i][j];
7+
matrix[i][j] = matrix[n - 1 -j][i];
8+
matrix[n - 1 -j][i] = matrix[n - 1 -i][n - 1 -j];
9+
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 -i];
10+
matrix[j][n - 1 -i] = temp;
11+
}
12+
}
13+
}
14+
15+
public static void main(String[] args) {
16+
Solution solution = new Solution();
17+
int[][] matrix = {
18+
{1, 2, 3},
19+
{4, 5, 6},
20+
{7, 8, 9}
21+
};
22+
23+
solution.rotate(matrix);
24+
25+
for (int i = 0; i < matrix.length; i++) {
26+
for (int j = 0; j < matrix[0].length; j++) {
27+
System.out.print(j == 0 ? matrix[i][j] : "," + matrix[i][j]);
28+
}
29+
System.out.println();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)