File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ # TC: O(m * n)
2
+ # SC: O(1)
3
+ class Solution :
4
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
5
+ rows , cols = len (matrix ), len (matrix [0 ])
6
+ first_row_zero = any (matrix [0 ][j ] == 0 for j in range (cols ))
7
+ first_col_zero = any (matrix [i ][0 ] == 0 for i in range (rows ))
8
+
9
+ # Use first row and column to mark zero rows and columns
10
+ for i in range (1 , rows ):
11
+ for j in range (1 , cols ):
12
+ if matrix [i ][j ] == 0 :
13
+ matrix [i ][0 ] = matrix [0 ][j ] = 0
14
+
15
+ # Set rows to zero based on marks in the first column
16
+ for i in range (1 , rows ):
17
+ if matrix [i ][0 ] == 0 :
18
+ for j in range (1 , cols ):
19
+ matrix [i ][j ] = 0
20
+
21
+ # Set columns to zero based on marks in the first row
22
+ for j in range (1 , cols ):
23
+ if matrix [0 ][j ] == 0 :
24
+ for i in range (1 , rows ):
25
+ matrix [i ][j ] = 0
26
+
27
+ # Handle the first row and column separately if needed
28
+ if first_row_zero :
29
+ for j in range (cols ):
30
+ matrix [0 ][j ] = 0
31
+
32
+ if first_col_zero :
33
+ for i in range (rows ):
34
+ matrix [i ][0 ] = 0
Original file line number Diff line number Diff line change
1
+ # TC: O(m * n)
2
+ # SC: O(m * n)
3
+ # where m is the number of the row and n is the number of columns
4
+ class Solution :
5
+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
6
+ rows , cols = len (matrix ), len (matrix [0 ])
7
+
8
+ row , col = 0 , - 1
9
+
10
+ direction = 1
11
+
12
+ result = []
13
+
14
+ while rows > 0 and cols > 0 :
15
+ for _ in range (cols ):
16
+ col += direction
17
+ result .append (matrix [row ][col ])
18
+ rows -= 1
19
+
20
+ for _ in range (rows ):
21
+ row += direction
22
+ result .append (matrix [row ][col ])
23
+ cols -= 1
24
+
25
+ direction *= - 1
26
+
27
+ return result
Original file line number Diff line number Diff line change
1
+ # TC : O(n)
2
+ # SC : O(n)
3
+ class Solution :
4
+ def getSum (self , a : int , b : int ) -> int :
5
+
6
+ mask = 0xFFFFFFFF
7
+
8
+ while (b & mask ) > 0 :
9
+
10
+ carry = (a & b ) << 1
11
+ a = a ^ b
12
+ b = carry
13
+
14
+ return (a & mask ) if b > 0 else a
You can’t perform that action at this time.
0 commit comments