Skip to content

Commit 4f7576b

Browse files
Merge pull request #6 from mihirs16/master
added image rotate | refactored repo
2 parents f4370ad + cf2f373 commit 4f7576b

6 files changed

+77
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/
2-
__pycache__/
2+
__pycache__/
3+
.DS_Store
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# rotate image | leetcode 48 | https://leetcode.com/problems/rotate-image/
2+
# You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
3+
# method: actual rotation
4+
5+
# testcase 1
6+
# matrix: list[list[int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
7+
8+
# testcase 2
9+
matrix: list[list[int]] = [[5, 1, 9, 11], [2, 4, 8, 10],[13, 3, 6, 7],[15, 14, 12, 16]]
10+
11+
def rotate(matrix: list[list[int]]) -> None:
12+
n: int = len(matrix) # size (n x n)
13+
left: int = 0 # left pointer
14+
right: int = n - 1 # right pointer
15+
top: int = 0 # top pointer
16+
bottom: int = 0 # bottom pointer
17+
temp: int = 0 # temp variable
18+
19+
# if left pointer is on
20+
# the right of the right pointer
21+
# stop iterating
22+
while left < right:
23+
24+
# for each square in layer
25+
for i in range(right - left):
26+
top: int = left
27+
bottom: int = right
28+
29+
temp = matrix[top][left + i] # save top-left
30+
matrix[top][left + i] = matrix[bottom - i][left] # bottom-left to top-left
31+
matrix[bottom - i][left] = matrix[bottom][right - i] # bottom-right to bottom-left
32+
matrix[bottom][right - i] = matrix[top + i][right] # top-right to bottom-right
33+
matrix[top + i][right] = temp # (saved) top-left to top-right
34+
35+
# next layer
36+
left = left + 1
37+
right = right - 1
38+
39+
# output
40+
rotate(matrix)
41+
print(matrix)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# rotate image | leetcode 48 | https://leetcode.com/problems/rotate-image/
2+
# You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
3+
# method: transpose + reflection
4+
5+
# testcase 1
6+
# matrix: list[list[int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
7+
8+
# testcase 2
9+
matrix: list[list[int]] = [[5, 1, 9, 11], [2, 4, 8, 10],[13, 3, 6, 7],[15, 14, 12, 16]]
10+
11+
def rotate(matrix: list[list[int]]) -> None:
12+
n: int = len(matrix)
13+
14+
# transpose of the matrix
15+
def transpose(matrix: list[list[int]]) -> None:
16+
for i in range(n):
17+
for j in range(i + 1, n):
18+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
19+
20+
# reflection of the matrix
21+
def reflect(matrix: list[list[int]]) -> None:
22+
for i in range(n):
23+
for j in range(n // 2):
24+
matrix[i][j], matrix[i][n - j - 1] = matrix[i][n - j - 1], matrix[i][j]
25+
26+
transpose(matrix)
27+
reflect(matrix)
28+
29+
# output
30+
rotate(matrix)
31+
print(matrix)

03. Data Structures/Hash Tables/Hash_Set.py renamed to 14. Questions/leetcode 705 - hash set.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# hashset | leetcode 705 | https://leetcode.com/problems/design-hashset/
2+
23
class HashSet:
34

45
# constructor

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@ All the essential resources and template code needed to understand and practice
7272
3. [Hash Project](/13.%20Mini-Projects/hash_project)
7373
4. [Recursion Miniprojects](/13.%20Mini-Projects/recursion_miniprojects)
7474
5. [Runtime Analyser](/13.%20Mini-Projects/runtime_analyser)
75+
76+
### 14. ❓ [Questions](/14.%20Questions/)

0 commit comments

Comments
 (0)