You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#You are given an n x n 2D matrix representing an image.
10
+
#
11
+
#Rotate the image by 90 degrees (clockwise).
12
+
#
13
+
#Note:
14
+
#
15
+
#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.
16
+
#
17
+
#Example 1:
18
+
#
19
+
#Given input matrix =
20
+
#[
21
+
# [1,2,3],
22
+
# [4,5,6],
23
+
# [7,8,9]
24
+
#],
25
+
#
26
+
#rotate the input matrix in-place such that it becomes:
27
+
#[
28
+
# [7,4,1],
29
+
# [8,5,2],
30
+
# [9,6,3]
31
+
#]
32
+
#Example 2:
33
+
#
34
+
#Given input matrix =
35
+
#[
36
+
# [ 5, 1, 9,11],
37
+
# [ 2, 4, 8,10],
38
+
# [13, 3, 6, 7],
39
+
# [15,14,12,16]
40
+
#],
41
+
#
42
+
#rotate the input matrix in-place such that it becomes:
43
+
#[
44
+
# [15,13, 2, 5],
45
+
# [14, 3, 4, 1],
46
+
# [12, 6, 8, 9],
47
+
# [16, 7,10,11]
48
+
#]
49
+
classSolution:
50
+
defrotate(self, matrix):
51
+
n=len(matrix[0]) #Initialize n which is length of matrix at 0 index
52
+
foriinrange(n): #Loop through n
53
+
forjinrange(i, n): #Loop through n starts from i
54
+
matrix[j][i], matrix[i][j] =matrix[i][j], matrix[j][i] #Inter change the values
55
+
foriinrange(n): #Loop through n
56
+
matrix[i].reverse() #Using inbuilt method to reverse the rows
0 commit comments