LeetCode 73 Set Matrix Zeroes

来源:互联网 发布:如何添加网络打印机 编辑:程序博客网 时间:2024/05/07 17:01

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

public void setZeroes2(int[][] matrix) {if (matrix == null || matrix.length == 0) return;int col0 = 1, rows = matrix.length, cols = matrix[0].length;for (int i = 0; i < rows; i++) {if (matrix[i][0] == 0) col0 = 0;for (int j = 1; j < cols; j++)if (matrix[i][j] == 0)matrix[i][0] = matrix[0][j] = 0;}for (int i = rows - 1; i >= 0; i--) {for (int j = cols - 1; j >= 1; j--)if (matrix[i][0] == 0 || matrix[0][j] == 0)matrix[i][j] = 0;if (col0 == 0) matrix[i][0] = 0;}}

0 0