-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetToZeros.py
59 lines (51 loc) · 1.63 KB
/
SetToZeros.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
'''
https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/29/array-and-strings/76/
'''
from typing import List
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
row = matrix.__len__()
if row < 1:
return
col = 0
col = matrix[0].__len__()
firstIsZero = False
firstRowIsZero = False
firstColIsZero = False
if matrix[0][0] == 0:
firstIsZero = True
for i in range(1,row):
if matrix[i][0] == 0:
firstColIsZero = True
matrix[i][0]=0
for j in range(1,col):
if matrix[0][j] == 0:
firstRowIsZero = True
matrix[0][j]=0
for i in range(1, row):
for j in range(1, col):
if matrix[i][j] == 0:
matrix[0][j] = 0
matrix[i][0] = 0
for i in range(1, row):
if matrix[i][0] == 0:
for j in range(col):
matrix[i][j] = 0
for j in range(1, col):
if matrix[0][j] ==0:
for i in range(row):
matrix[i][j] = 0
if firstIsZero or firstColIsZero:
for i in range(row):
matrix[i][0] = 0
if firstIsZero or firstRowIsZero:
for j in range(col):
matrix[0][j] = 0
if __name__ == '__main__':
s = Solution()
m = [[0,0,0,5],[4,3,1,4],[0,1,1,4],[1,2,1,3],[0,0,1,1]]
s.setZeroes(m)
print(m)
m=[[-4,-2147483648,6,-7,0],[-8,6,-8,-6,0],[2147483647,2,-9,-6,-10]]
s.setZeroes(m)
print(m)