-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
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
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,3 +1,4 @@ | ||
20240304: 73 | ||
20240303: 55(again),56,62,70 | ||
20240229: 39,48(again),49,53,54 | ||
20240228: 20,21,23,33(again) | ||
|
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
87 changes: 87 additions & 0 deletions
87
leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package LeetCodeJava.Array; | ||
|
||
// https://leetcode.com/problems/set-matrix-zeroes/description/ | ||
|
||
public class SetMatrixZeroes { | ||
|
||
// V0 | ||
// IDEA : ARRAY OP | ||
// TODO: implement below | ||
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/set-matrix-zeroes.py | ||
public void setZeroes(int[][] matrix) { | ||
|
||
} | ||
|
||
// V1 | ||
public void setZeroes_1(int[][] matrix) { | ||
boolean fr = false,fc = false; | ||
for(int i = 0; i < matrix.length; i++) { | ||
for(int j = 0; j < matrix[0].length; j++) { | ||
if(matrix[i][j] == 0) { | ||
if(i == 0) fr = true; | ||
if(j == 0) fc = true; | ||
matrix[0][j] = 0; | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
for(int i = 1; i < matrix.length; i++) { | ||
for(int j = 1; j < matrix[0].length; j++) { | ||
if(matrix[i][0] == 0 || matrix[0][j] == 0) { | ||
matrix[i][j] = 0; | ||
}} | ||
} | ||
if(fr) { | ||
for(int j = 0; j < matrix[0].length; j++) { | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
if(fc) { | ||
for(int i = 0; i < matrix.length; i++) { | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
|
||
// V2 | ||
// https://leetcode.com/problems/set-matrix-zeroes/solutions/4800467/using-2-d-boolean-array/ | ||
public void setZeroes_2(int[][] matrix) { | ||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
boolean[][] isOriginalZero = new boolean[rows][cols]; | ||
|
||
for(int i=0;i<rows;i++){ | ||
for(int j=0;j<cols;j++){ | ||
if(matrix[i][j] == 0) | ||
isOriginalZero[i][j] = true; | ||
else | ||
continue; | ||
} | ||
} | ||
|
||
for(int i=0;i<rows;i++){ | ||
for(int j=0;j<cols;j++){ | ||
int currRow = i; | ||
int currCol = j; | ||
if(matrix[currRow][currCol]==0 && isOriginalZero[currRow][currCol]){ | ||
for(int c=0;c<currCol;c++){ | ||
matrix[currRow][c] = 0; | ||
} | ||
for(int c=currCol;c<cols;c++){ | ||
matrix[currRow][c] = 0; | ||
} | ||
|
||
for(int r=0;r<currRow;r++){ | ||
matrix[r][currCol] = 0; | ||
} | ||
for(int r=currRow;r<rows;r++){ | ||
matrix[r][currCol] = 0; | ||
} | ||
} | ||
else | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
} |