Skip to content

Commit

Permalink
https://leetcode.com/problems/set-matrix-zeroes/
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Dec 24, 2023
1 parent f1dee07 commit 4e609b7
Showing 1 changed file with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import java.util.Arrays;

class Solution {
public void setZeroes(int[][] m) {
m[0][0] = 9999;
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean[] rows = new boolean[m];
boolean[] cols = new boolean[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 0) {
rows[i] = true;
cols[j] = true;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (rows[i] || cols[j]) {
matrix[i][j] = 0;
}
}
}
}

public static void main(String[] args) {
Solution self = new Solution();
int[][] i1 = new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}};
int[][] o1 = new int[][]{{0,0,0,0},{0,4,5,0},{0,3,1,0}};

self.setZeroes(i1);
//https://leetcode.com/problems/set-matrix-zeroes/
test(
new int[][]{{1,1,1}, {1,0,1},{1,1,1}},
new int[][]{{1,0,1},{0,0,0},{1,0,1}}
);

assert Arrays.equals(i1, o1) == true;
test(
new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}},
new int[][]{{0,0,0,0},{0,4,5,0},{0,3,1,0}}
);
}
static void test(int[][] in, int[][] out){
Solution self = new Solution();
self.setZeroes(in);
assert Arrays.deepEquals(in, out) == true;
}
}

0 comments on commit 4e609b7

Please sign in to comment.