-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path047 Rotate Image.py
More file actions
32 lines (24 loc) · 869 Bytes
/
047 Rotate Image.py
File metadata and controls
32 lines (24 loc) · 869 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
"""
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Author: Rajeev Ranjan
"""
class Solution:
def rotate(self, matrix):
"""
rotate matrix n*n
1. flip along the diagonal
2. flip along x-axis
:param matrix: a list of lists of integers
:return: a list of lists of integers
"""
n = len(matrix)
for row in range(n):
for col in range(n-row):
matrix[row][col], matrix[n-1-col][n-1-row] = matrix[n-1-col][n-1-row], matrix[row][col] # by diagonal
for row in range(n/2):
for col in range(n):
matrix[row][col], matrix[n-1-row][col] = matrix[n-1-row][col], matrix[row][col] # by x-axis
return matrix