Skip to content

Commit 501bdaf

Browse files
committed
Leetcode accepted
1 parent 91b049e commit 501bdaf

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

Blind 75/Programs/Rotate Image.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Rotate Image
2+
# https://leetcode.com/problems/rotate-image/
3+
4+
class Solution(object):
5+
def diagonalSwap(self, matrix):
6+
colBound = 0
7+
rowBound = 0
8+
# extra minus one is from preventing the diagonal getting swapped
9+
while(rowBound < len(matrix)-1):
10+
# swap cells, we wanna swap row, col with len-1-col, len-1-row
11+
matrix[rowBound][colBound], matrix[len(matrix[0])-1-colBound][len(matrix)-1-rowBound] = matrix[len(matrix[0])-1-colBound][len(matrix)-1-rowBound], matrix[rowBound][colBound]
12+
# increment the col value
13+
colBound = colBound + 1
14+
# extra minus row bound is from preventing the diagonal from getting swapped.
15+
if (colBound == len(matrix[0])-1-rowBound):
16+
colBound = 0
17+
rowBound = rowBound + 1
18+
19+
def inverseMatrix(self, matrix):
20+
for row in range(len(matrix)/2):
21+
# swap first row with last
22+
matrix[row], matrix[len(matrix)-1-row] = matrix[len(matrix)-1-row], matrix[row]
23+
24+
def rotate(self, matrix):
25+
self.diagonalSwap(matrix)
26+
self.inverseMatrix(matrix)
27+
"""
28+
:type matrix: List[List[int]]
29+
:rtype: None Do not return anything, modify matrix in-place instead.
30+
"""
31+

Blind 75/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>Blind 75</h1>
22
<a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">Leetcode link to the post.</a>
3-
<h4>Total Completed: 27</h4>
3+
<h4>Total Completed: 28</h4>
44
<h2>Array</h2>
55
<ul>
66
<li><a href="Programs/Two Sum.py">Two Sum</a>
@@ -207,7 +207,12 @@
207207
Don't forget the mark the cells visited to avoid duplicates.
208208
</p>
209209
</li>
210-
<li>Rotate Image</li>
210+
<li><a href="Programs/Rotate Image.py">Rotate Image</a>
211+
<p><b>Approach</b>: First swap the elements under red triangle with the green triangle.<br/>
212+
<img src="img/Diagonal-Swap.png" height="200" width="200"><br/>
213+
Then flip the matrix upside down.
214+
</p>
215+
</li>
211216
<li>Word Search</li>
212217
</ul>
213218
<h2>String</h2>

Blind 75/img/Diagonal-Swap.png

260 KB
Loading

0 commit comments

Comments
 (0)