-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetMatrixZeros_leetcode.py
101 lines (93 loc) · 2.44 KB
/
setMatrixZeros_leetcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/777/
# https://leetcode.com/problems/set-matrix-zeroes/discuss/26115/JavaPython-O(1)-space-11-lines-solution
#
#Input:
# [[1,1,1],
# [1,0,1],
# [1,1,1]]
#Output:
#[[1,0,1],
# [0,0,0],
# [1,0,1]]
#
class Solution(object):
# imolementation 1
def matrixZeros(self, matrix):
rowFlag = False
colFlag = False
rows = len(matrix)-1
cols = len(matrix[0])-1
print("rows and cols ", rows,cols)
for i in range(0, rows):
for j in range(0, cols):
if i == 0 and matrix[i][j] == 0:
print("i ", i)
rowFlag = True
if j == 0 and matrix[i][j] == 0:
print("j ", j)
colFlag = True
if matrix[i][j] == 0:
print("i and j", i, j)
matrix[0][j] = 0
matrix[i][0] = 0
for i in range(0, rows):
for j in range(0, cols):
if matrix[0][j] == 0 or matrix[i][0] == 0:
print("i and j 2 -- ", i, j)
matrix[i][j] = 0
if rowFlag == True:
for i in range(0, cols):
print("cols ", i)
matrix[0][cols] = 0
if colFlag == True:
for i in range(0, rows):
print("rows ", i)
matrix[rows][0] = 0
return matrix
# using set()
# O(m*n) time complexity
def matrixZeros_(self, matrix):
rowID = set()
colID = set()
rows = len(matrix)
cols = len(matrix[0])
for i in range(0, rows):
for j in range(0, cols):
if matrix[i][j] == 0:
rowID.add(i)
colID.add(j)
for i in range(0, rows):
for j in range(0, cols):
if i in rowID or j in colID:
matrix[i][j] =0
return matrix
def setZeroes(self, matrix):
rows = set()
columns = set()
#First pass to check which rows and columns will need to be changed
for y in range(len(matrix)):
for x in range(len(matrix[0])):
if matrix[y][x] == 0:
rows.add(y)
columns.add(x)
#Second pass to change rows and columns to zero values
for y in range(len(matrix)):
for x in range(len(matrix[0])):
if y in rows or x in columns:
matrix[y][x] = 0
return matrix
#######
matrix = [
[1,1,1],
[1,0,1],
[1,1,1]
]
matrix_ = [
[0,1,2,0],
[3,4,5,2],
[1,3,1,5],
[1, 2, 0, 0]
]
s = Solution()
print(s.setZeroes(matrix_))
#print(s.matrixZeros1(matrix_))