diff --git a/Java/set-matrix-zeroes.java b/Java/set-matrix-zeroes.java new file mode 100644 index 00000000..678f0897 --- /dev/null +++ b/Java/set-matrix-zeroes.java @@ -0,0 +1,34 @@ +class Solution { + public void setZeroes(int[][] matrix) { + Boolean isCol = false; + int R = matrix.length, C = matrix[0].length; + for (int i = 0; i < R; i++) { + if (matrix[i][0] == 0) { + isCol = true; + } + for (int j = 1; j < C; j++) { + if (matrix[i][j] == 0) { + matrix[0][j] = 0; + matrix[i][0] = 0; + } + } + } + for (int i = 1; i < R; i++) { + for (int j = 1; j < C; j++) { + if (matrix[i][0] == 0 || matrix[0][j] == 0) { + matrix[i][j] = 0; + } + } + } + if (matrix[0][0] == 0) { + for (int j = 0; j < C; j++) { + matrix[0][j] = 0; + } + } + if (isCol) { + for (int i = 0; i < R; i++) { + matrix[i][0] = 0; + } + } + } +} diff --git a/README.md b/README.md index 66065bc0..976711aa 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | [Python](./python/SmallestDifference.py) | O(n) | O(1) | Easy | Array | | 532 | [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [C++](./C++/k-diff-pairs-in-an-array.cpp) | O(n) | O(n) | Medium | Array | | 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Javascript](./JavaScript/152.Maximum-Product-Subarray.js) | O(n) | O(n) | Medium | Array | +| 073 | [Set-Matrix-Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Java](./Java/set-matrix-zeroes.java) | O(MN) | O(1) | Medium | Array |