diff --git a/interview_prep/algorithm/java/ide_handicapped/set_matrix_zeroes/Solution.java b/interview_prep/algorithm/java/ide_handicapped/set_matrix_zeroes/Solution.java index 5f6a275..d2f3e22 100644 --- a/interview_prep/algorithm/java/ide_handicapped/set_matrix_zeroes/Solution.java +++ b/interview_prep/algorithm/java/ide_handicapped/set_matrix_zeroes/Solution.java @@ -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; } } \ No newline at end of file