编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
示例 1:
输入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
输出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例 2:
输入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
输出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
var setZeroes = function(matrix) {
const m = matrix.length, n = matrix[0].length
let row0 = false, col0 = false
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) {
col0 = true
break;
}
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) {
row0 = true
break;
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) matrix[i][j] = 0
}
}
if (col0) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0
}
}
if (row0) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0
}
}
};
function setZeroes(matrix: number[][]): void {
const m = matrix.length, n = matrix[0].length
let col0 = false
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) col0 = true
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}
for (let i = m; i--;) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) matrix[i][j] = 0
}
if (col0) matrix[i][0] = 0
}
};
class Solution {
function setZeroes(&$matrix) {
$m = count($matrix);
$n = count($matrix[0]);
$col0 = false;
for ($i = 0; $i < $m; $i++) {
if ($matrix[$i][0] === 0) $col0 = true;
for ($j = 1; $j < $n; $j++) {
if ($matrix[$i][$j] === 0) {
$matrix[$i][0] = 0;
$matrix[0][$j] = 0;
}
}
}
for ($i = $m; $i--;) {
for ($j = 1; $j < $n; $j++) {
if ($matrix[$i][0] === 0 || $matrix[0][$j] === 0) $matrix[$i][$j] = 0;
}
if ($col0 == true) {
$matrix[$i][0] = 0;
}
}
}
}
func setZeroes(matrix [][]int) {
m, n, col0 := len(matrix), len(matrix[0]), false
for i := 0; i < m; i++ {
if matrix[i][0] == 0 {
col0 = true
}
for j := 1; j < n; j++ {
if matrix[i][j] == 0 {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}
for i := m - 1; i >= 0; i-- {
for j := 1; j < n; j++ {
if matrix[i][0] == 0 || matrix[0][j] == 0 {
matrix[i][j] = 0
}
}
if col0 {
matrix[i][0] = 0;
}
}
}
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean col0 = false;
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) col0 = true;
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
}
if (col0) matrix[i][0] = 0;
}
}
}
public class Solution {
public void SetZeroes(int[][] matrix) {
int m = matrix.Length, n = matrix[0].Length;
bool col0 = false;
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) col0 = true;
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
}
if (col0) matrix[i][0] = 0;
}
}
}
void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
bool col0 = false;
for (int i = 0; i < matrixSize; i++) {
if (matrix[i][0] == 0) col0 = true;
for (int j = 1; j < *matrixColSize; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = matrixSize; i--;) {
for (int j = 1; j < *matrixColSize; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
if (col0) matrix[i][0] = 0;
}
}
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
bool col0 = false;
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) col0 = true;
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = m; i--;) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
}
if (col0) matrix[i][0] = 0;
}
}
};
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
m, n, col0 = len(matrix), len(matrix[0]), False
for i in range(0, m):
if matrix[i][0] == 0: col0 = True
for j in range(1, n):
if matrix[i][j] == 0: matrix[i][0], matrix[0][j] = 0, 0
for i in range(m - 1, -1, -1):
for j in range(1, n):
if matrix[i][0] == 0 or matrix[0][j] == 0: matrix[i][j] = 0
if col0: matrix[i][0] = 0
var setZeroes = function(matrix) {
const m = matrix.length, n = matrix[0].length
let row0 = false
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) row0 = true
for (let i = 1; i < m; i++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}
for (let j = n; j--;) {
for (let i = 1; i < m; i++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) matrix[i][j] = 0
}
if (row0) matrix[0][j] = 0
}
};