-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode.com/problems/set-matrix-zeroes/
- Loading branch information
Showing
1 changed file
with
33 additions
and
8 deletions.
There are no files selected for viewing
41 changes: 33 additions & 8 deletions
41
interview_prep/algorithm/java/ide_handicapped/set_matrix_zeroes/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |