-
Notifications
You must be signed in to change notification settings - Fork 18
Solution #73 - Jabez/Edited - 17/3/2025 #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ByteSurfer23
wants to merge
2
commits into
Dijkstra-Edu:master
Choose a base branch
from
ByteSurfer23:feature/set-matrix-zeroes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Explanation.md
This file contains hidden or 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,29 @@ | ||
What This Code Does: | ||
* Find zeros in the matrix | ||
|
||
* If a cell is 0, mark its row and column to be zero later. | ||
* Use the first row & column as markers | ||
|
||
* To track if first row and column have zeros we use firstrow and firstcol boolean variables | ||
|
||
* Instead of creating extra memory, the first row and column store which rows/columns should be zero. | ||
* Set the marked cells to zero | ||
|
||
* Go through the matrix and make elements 0 based on the markers. | ||
* Handle the first row and column separately | ||
|
||
If the first row/column originally had a 0, set the entire first row/column to 0. | ||
|
||
Imagine this matrix: | ||
|
||
|
||
1 2 3 | ||
4 0 6 | ||
7 8 9 | ||
|
||
After running the code, it becomes: | ||
|
||
1 0 3 | ||
0 0 0 | ||
7 0 9 | ||
The 0 at (1,1) caused its entire row and column to be zero. | ||
42 changes: 42 additions & 0 deletions
42
Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Solution.java
This file contains hidden or 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,42 @@ | ||
class Solution{ | ||
public void SetMatrixZeroes(int[][] matrix) { | ||
boolean firstrow = false, firstcol = false; // Used to check if the first row and first column contain any zeros | ||
|
||
// Iterate through the matrix to find zeros | ||
for (int i = 0; i < matrix.length; i++) { | ||
for (int j = 0; j < matrix[0].length; j++) { | ||
if (matrix[i][j] == 0) { // If an element is 0, mark its row and column in the first row and first column | ||
if (i == 0) firstrow = true; // Mark that the first row has a zero | ||
if (j == 0) firstcol = true; // Mark that the first column has a zero | ||
|
||
// Store zero markers in the first row and first column | ||
matrix[i][0] = 0; | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
// Use the markers in the first row and column to set matrix elements to zero | ||
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 the first row was marked, set all elements in the first row to zero | ||
if (firstrow) { | ||
for (int j = 0; j < matrix[0].length; j++) { | ||
matrix[0][j] = 0; | ||
} | ||
} | ||
|
||
// If the first column was marked, set all elements in the first column to zero | ||
if (firstcol) { | ||
for (int i = 0; i < matrix.length; i++) { | ||
matrix[i][0] = 0; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem too clear. Could you try doing something like this? https://github.com/Dijkstra-Edu/LeetCode-Solutions/pull/16/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also use the template File for your explanation: https://github.com/Dijkstra-Edu/LeetCode-Solutions/blob/master/Explanation-Template.md