Skip to content

Commit 3028e8f

Browse files
authored
Merge pull request #1487 from ivan1016017/december16
adding algo
2 parents 309aa52 + 3eccb29 commit 3028e8f

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def rotate(self, matrix: List[List[int]]) -> None:
6+
7+
"""
8+
:type matrix: List[List[int]]
9+
:rtype: void Do not return anything, modify matrix in-place instead.
10+
"""
11+
12+
n = len(matrix)
13+
14+
for i in range(n):
15+
for j in range(i+1,n):
16+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
17+
18+
for r in matrix:
19+
r.reverse()
20+
21+
return matrix
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def setZeroes(self, matrix: List[List[int]]) -> None:
6+
"""
7+
Do not return anything, modify matrix in-place instead.
8+
"""
9+
if not matrix or not matrix[0]:
10+
return
11+
12+
m, n = len(matrix), len(matrix[0])
13+
14+
# Flags to track if first row/column should be zeroed
15+
first_row_zero = False
16+
first_col_zero = False
17+
18+
# Check if first row has any zeros
19+
for j in range(n):
20+
if matrix[0][j] == 0:
21+
first_row_zero = True
22+
break
23+
24+
# Check if first column has any zeros
25+
for i in range(m):
26+
if matrix[i][0] == 0:
27+
first_col_zero = True
28+
break
29+
30+
# Use first row and column as markers
31+
# Check rest of matrix and mark first row/column
32+
for i in range(1, m):
33+
for j in range(1, n):
34+
if matrix[i][j] == 0:
35+
matrix[i][0] = 0 # Mark row
36+
matrix[0][j] = 0 # Mark column
37+
38+
# Set zeros based on markers (skip first row/column)
39+
for i in range(1, m):
40+
for j in range(1, n):
41+
if matrix[i][0] == 0 or matrix[0][j] == 0:
42+
matrix[i][j] = 0
43+
44+
# Handle first row
45+
if first_row_zero:
46+
for j in range(n):
47+
matrix[0][j] = 0
48+
49+
# Handle first column
50+
if first_col_zero:
51+
for i in range(m):
52+
matrix[i][0] = 0
53+
54+
return matrix
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_35_rotate_image import Solution
4+
5+
class RotateImageTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.rotate(matrix = [[1,2,3],[4,5,6],[7,8,9]])
10+
target = [[7,4,1],[8,5,2],[9,6,3]]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.rotate(matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]])
16+
target = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
17+
self.assertEqual(output, target)
18+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_36_set_matrix_zeroes import Solution
4+
5+
class MatrixZeroesTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.setZeroes(matrix = [[1,1,1],[1,0,1],[1,1,1]])
10+
target = [[1,0,1],[0,0,0],[1,0,1]]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.setZeroes(matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]])
16+
target = [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
17+
self.assertEqual(output, target)
18+

0 commit comments

Comments
 (0)