-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_RotateMatrix.py
More file actions
45 lines (22 loc) · 880 Bytes
/
02_RotateMatrix.py
File metadata and controls
45 lines (22 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Question link - https://leetcode.com/problems/rotate-image/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
# Transpose
for i in range(n):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i] , matrix[i][j]
# Reverse
for i in range(n):
for j in range(n//2):
matrix[i][j] , matrix[i][n-1-j] = matrix[i][n-1-j] , matrix[i][j]
# Brute Approch
# ans = [[0]* n for _ in range(n)]
# for i in range(n):
# for j in range(n):
# ans[j][n-i-1] = matrix[i][j]
# for i in range(n):
# matrix[i] = ans[i]