Skip to content

Instantly share code, notes, and snippets.

@han8909227
Last active December 8, 2017 17:36
Show Gist options
  • Save han8909227/83bd59bba40031f8740242c060992199 to your computer and use it in GitHub Desktop.
Save han8909227/83bd59bba40031f8740242c060992199 to your computer and use it in GitHub Desktop.
Zero Matrix
def zero_matrix(matrix):
"""Take a matrix m x n, if any val is zero set the entire row/column to zero"""
zero_loc = []
for j in range(len(matrix)):
for i in range(len(matrix[0])):
if matrix[j][i] == 0:
zero_loc.push((j,i))
for j, i in zero_loc:
matrix[j] = [0 for val in range(len(matrix[j])]
for row in matrix:
matrix[row][i] = 0
return matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment