Skip to content

Commit debae72

Browse files
committed
Adding Simple Solution for Problem - 48 - Rotate Image
1 parent b980907 commit debae72

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Rotate_Image/SimpleSolution.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 48
6+
## Problem Name: Rotate Image
7+
##===================================
8+
#
9+
#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+
class Solution:
50+
def rotate(self, matrix):
51+
n = len(matrix[0]) #Initialize n which is length of matrix at 0 index
52+
for i in range(n): #Loop through n
53+
for j in range(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+
for i in range(n): #Loop through n
56+
matrix[i].reverse() #Using inbuilt method to reverse the rows

0 commit comments

Comments
 (0)