59 lines
1.6 KiB
Java
59 lines
1.6 KiB
Java
package com.zerroi.leetcode.Three17;
|
|
|
|
public class SetZeroes {
|
|
public static void main(String[] args) {
|
|
SolutionSecond solutionSecond = new SolutionSecond();
|
|
solutionSecond.setZeroes(new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}});
|
|
|
|
}
|
|
}
|
|
|
|
class SolutionSecond {
|
|
public void setZeroes(int[][] matrix) {
|
|
int m = matrix.length;
|
|
int n = matrix[0].length;
|
|
int[][] position = new int[m][n];
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
if (matrix[i][j] == 0) position[i][j] = 1;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
if (position[i][j] == 1) {
|
|
for (int k = 0; k < n; k++) {
|
|
if (matrix[i][k] != 0) matrix[i][k] = 0;
|
|
}
|
|
for (int l = 0; l < n; l++) {
|
|
if (matrix[l][j] != 0) matrix[l][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void setZeroes(int[][] matrix, int a) {
|
|
int m = matrix.length;
|
|
int n = matrix[0].length;
|
|
boolean[] col = new boolean[m];
|
|
boolean[] row = new boolean[n];
|
|
for (int i = 0; i < m; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
if (matrix[i][j] == 0) {
|
|
col[i] = row[j] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
if (col[i] || row[j] ) {
|
|
matrix[i][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |