Skip to content

Commit ef72660

Browse files
authored
Create 48_Rotate_Image.md
1 parent 7f52b59 commit ef72660

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

48_Rotate_Image.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 48. Rotate Image
2+
3+
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
4+
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.
5+
6+
7+
```
8+
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
9+
Output: [[7,4,1],[8,5,2],[9,6,3]]
10+
11+
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
12+
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
13+
14+
Input: matrix = [[1]]
15+
Output: [[1]]
16+
17+
Input: matrix = [[1,2],[3,4]]
18+
Output: [[3,1],[4,2]]
19+
```
20+
21+
Constraints:
22+
23+
matrix.length == n
24+
matrix[i].length == n
25+
1 <= n <= 20
26+
-1000 <= matrix[i][j] <= 1000
27+
28+
29+
```python
30+
for i in range(len(matrix)):
31+
for j in range(i, len(matrix)):
32+
matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j]
33+
34+
35+
for i in range(len(matrix)):
36+
left=0
37+
right=len(matrix)-1
38+
39+
while left < right:
40+
matrix[i][left], matrix[i][right] = matrix[i][right], matrix[i][left]
41+
left+=1
42+
right-=1
43+
```
44+
45+
```
46+
Runtime: 32 ms, faster than 81.03% of Python3 online submissions for Rotate Image.
47+
Memory Usage: 14.4 MB, less than 9.01% of Python3 online submissions for Rotate Image.
48+
```

0 commit comments

Comments
 (0)