Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df815e8

Browse files
solves set matrix zeroes
1 parent 3d9040f commit df815e8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| 67 | [Add Binary](https://leetcode.com/problems/add-binary) | [![Java](assets/java.png)](src/AddBinary.java) [![Python](assets/python.png)](python/add_binary.py) | |
6767
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx) | [![Java](assets/java.png)](src/Sqrtx.java) [![Python](assets/python.png)](python/sqrt.py) | |
6868
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [![Java](assets/java.png)](src/ClimbingStairs.java) [![Python](assets/python.png)](python/climbing_stairs.py) | |
69+
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes) | [![Java](assets/java.png)](src/SetMatrixZeroes.java) | |
6970
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
7071
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
7172
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |

‎src/SetMatrixZeroes.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// https://leetcode.com/problems/set-matrix-zeroes/
2+
// T: O(m * n)
3+
// S: O(1)
4+
5+
public class SetMatrixZeroes {
6+
public void setZeroes(int[][] matrix) {
7+
final int rows = matrix.length, columns = matrix[0].length;
8+
boolean firstColumnIsZero = false;
9+
10+
for (int row = 0 ; row < rows ; row++) {
11+
if (matrix[row][0] == 0) firstColumnIsZero = true;
12+
for (int column = 1 ; column < columns ; column++) {
13+
if (matrix[row][column] == 0) {
14+
matrix[row][0] = 0;
15+
matrix[0][column] = 0;
16+
}
17+
}
18+
}
19+
20+
for (int column = columns - 1 ; column >= 1 ; column--) {
21+
if (matrix[0][column] == 0) {
22+
markColumn0(matrix, column);
23+
}
24+
}
25+
26+
for (int row = rows - 1 ; row >= 0 ; row--) {
27+
if (matrix[row][0] == 0) {
28+
markRow0(matrix, row);
29+
}
30+
}
31+
32+
if (firstColumnIsZero) markColumn0(matrix, 0);
33+
}
34+
35+
private void markRow0(int[][] matrix, int row) {
36+
for (int column = 0 ; column < matrix[0].length ; column++) {
37+
matrix[row][column] = 0;
38+
}
39+
}
40+
41+
private void markColumn0(int[][] matrix, int column) {
42+
for (int row = 0 ; row < matrix.length ; row++) {
43+
matrix[row][column] = 0;
44+
}
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /