File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 66
66
| 67 | [ Add Binary] ( https://leetcode.com/problems/add-binary ) | [ ![ Java] ( assets/java.png )] ( src/AddBinary.java ) [ ![ Python] ( assets/python.png )] ( python/add_binary.py ) | |
67
67
| 69 | [ Sqrt(x)] ( https://leetcode.com/problems/sqrtx ) | [ ![ Java] ( assets/java.png )] ( src/Sqrtx.java ) [ ![ Python] ( assets/python.png )] ( python/sqrt.py ) | |
68
68
| 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 ) | |
69
70
| 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 ) | |
70
71
| 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 ) | |
71
72
| 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 ) | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments