Skip to content

Commit af240ee

Browse files
committed
feat: add python and java solutions to lcci problem
添加《程序员面试金典》题解:面试题 01.07. 旋转矩阵
1 parent 51f555a commit af240ee

File tree

4 files changed

+188
-67
lines changed

4 files changed

+188
-67
lines changed

lcci/01.07.Rotate Matrix/README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,46 @@
4848
## 解法
4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
原地旋转,i 的范围是 `[0, n/2)`,j 的范围是 `[i, n-1-i)`
5152

5253
### Python3
5354
<!-- 这里可写当前语言的特殊实现逻辑 -->
5455

5556
```python
57+
class Solution:
58+
def rotate(self, matrix: List[List[int]]) -> None:
59+
"""
60+
Do not return anything, modify matrix in-place instead.
61+
"""
62+
n = len(matrix)
63+
for i in range(n // 2):
64+
for j in range(i, n - 1 - i):
65+
t = matrix[i][j]
66+
matrix[i][j] = matrix[n - j - 1][i]
67+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
68+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
69+
matrix[j][n - i - 1] = t
5670

5771
```
5872

5973
### Java
6074
<!-- 这里可写当前语言的特殊实现逻辑 -->
6175

6276
```java
63-
77+
class Solution {
78+
public void rotate(int[][] matrix) {
79+
int n = matrix.length;
80+
for (int i = 0; i < n / 2; ++i) {
81+
for (int j = i; j < n - 1 - i; ++j) {
82+
int t = matrix[i][j];
83+
matrix[i][j] = matrix[n - j - 1][i];
84+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
85+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
86+
matrix[j][n - i - 1] = t;
87+
}
88+
}
89+
}
90+
}
6491
```
6592

6693
### ...

lcci/01.07.Rotate Matrix/README_EN.md

+133-66
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,133 @@
1-
# [01.07. Rotate Matrix](https://leetcode-cn.com/problems/rotate-matrix-lcci)
2-
3-
## Description
4-
<p>Given an image represented by an N x N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example 1:</strong></p>
9-
10-
<pre>
11-
Given <strong>matrix</strong> =
12-
[
13-
[1,2,3],
14-
[4,5,6],
15-
[7,8,9]
16-
],
17-
18-
Rotate the matrix <strong>in place. </strong>It becomes:
19-
[
20-
[7,4,1],
21-
[8,5,2],
22-
[9,6,3]
23-
]
24-
</pre>
25-
26-
<p><strong>Example 2:</strong></p>
27-
28-
<pre>
29-
Given <strong>matrix</strong> =
30-
[
31-
[ 5, 1, 9,11],
32-
[ 2, 4, 8,10],
33-
[13, 3, 6, 7],
34-
[15,14,12,16]
35-
],
36-
37-
Rotate the matrix <strong>in place. </strong>It becomes:
38-
[
39-
[15,13, 2, 5],
40-
[14, 3, 4, 1],
41-
[12, 6, 8, 9],
42-
[16, 7,10,11]
43-
]
44-
</pre>
45-
46-
47-
48-
## Solutions
49-
50-
51-
### Python3
52-
53-
```python
54-
55-
```
56-
57-
### Java
58-
59-
```java
60-
61-
```
62-
63-
### ...
64-
```
65-
66-
```
1+
# [01.07. Rotate Matrix](https://leetcode-cn.com/problems/rotate-matrix-lcci)
2+
3+
## Description
4+
<p>Given an image represented by an N x N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example 1:</strong></p>
13+
14+
15+
16+
<pre>
17+
18+
Given <strong>matrix</strong> =
19+
20+
[
21+
22+
[1,2,3],
23+
24+
[4,5,6],
25+
26+
[7,8,9]
27+
28+
],
29+
30+
31+
32+
Rotate the matrix <strong>in place. </strong>It becomes:
33+
34+
[
35+
36+
[7,4,1],
37+
38+
[8,5,2],
39+
40+
[9,6,3]
41+
42+
]
43+
44+
</pre>
45+
46+
47+
48+
<p><strong>Example 2:</strong></p>
49+
50+
51+
52+
<pre>
53+
54+
Given <strong>matrix</strong> =
55+
56+
[
57+
58+
[ 5, 1, 9,11],
59+
60+
[ 2, 4, 8,10],
61+
62+
[13, 3, 6, 7],
63+
64+
[15,14,12,16]
65+
66+
],
67+
68+
69+
70+
Rotate the matrix <strong>in place. </strong>It becomes:
71+
72+
[
73+
74+
[15,13, 2, 5],
75+
76+
[14, 3, 4, 1],
77+
78+
[12, 6, 8, 9],
79+
80+
[16, 7,10,11]
81+
82+
]
83+
84+
</pre>
85+
86+
87+
88+
89+
## Solutions
90+
91+
92+
### Python3
93+
94+
```python
95+
class Solution:
96+
def rotate(self, matrix: List[List[int]]) -> None:
97+
"""
98+
Do not return anything, modify matrix in-place instead.
99+
"""
100+
n = len(matrix)
101+
for i in range(n // 2):
102+
for j in range(i, n - 1 - i):
103+
t = matrix[i][j]
104+
matrix[i][j] = matrix[n - j - 1][i]
105+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
106+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
107+
matrix[j][n - i - 1] = t
108+
109+
```
110+
111+
### Java
112+
113+
```java
114+
class Solution {
115+
public void rotate(int[][] matrix) {
116+
int n = matrix.length;
117+
for (int i = 0; i < n / 2; ++i) {
118+
for (int j = i; j < n - 1 - i; ++j) {
119+
int t = matrix[i][j];
120+
matrix[i][j] = matrix[n - j - 1][i];
121+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
122+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
123+
matrix[j][n - i - 1] = t;
124+
}
125+
}
126+
}
127+
}
128+
```
129+
130+
### ...
131+
```
132+
133+
```
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 t = matrix[i][j];
7+
matrix[i][j] = matrix[n - j - 1][i];
8+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
9+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
10+
matrix[j][n - i - 1] = t;
11+
}
12+
}
13+
}
14+
}

lcci/01.07.Rotate Matrix/Solution.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def rotate(self, matrix: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify matrix in-place instead.
5+
"""
6+
n = len(matrix)
7+
for i in range(n // 2):
8+
for j in range(i, n - 1 - i):
9+
t = matrix[i][j]
10+
matrix[i][j] = matrix[n - j - 1][i]
11+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
12+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
13+
matrix[j][n - i - 1] = t

0 commit comments

Comments
 (0)