-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path73_SetMatrixZeroes.py
executable file
·52 lines (44 loc) · 1.42 KB
/
73_SetMatrixZeroes.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
if not matrix:
return []
m = len(matrix)
n = len(matrix[0])
# Frist, make sure whether first row and first col is all 0.
first_row = False
for i in range(n):
if matrix[0][i] == 0:
first_row = True
first_col = False
for j in range(m):
if matrix[j][0] == 0:
first_col = True
# Keep the information about the 0 cell to first row and first col.
for row in range(1, m):
for col in range(1, n):
if matrix[row][col] == 0:
matrix[row][0] = 0
matrix[0][col] = 0
# Set 0s according to the information in first row and first col
for row in range(m):
for col in range(n):
if not matrix[row][0] or not matrix[0][col]:
matrix[row][col] = 0
# Set the first row and first col
if first_row:
for col in range(n):
matrix[0][col] = 0
if first_col:
for row in range(m):
matrix[row][0] = 0
"""
[[0]]
[[1,0],[2,2]]
[[0,0,0,5],[4,3,1,4],[0,1,1,4],[1,2,1,3],[0,0,1,1]]
"""