Skip to content

Commit e3ee334

Browse files
Solve Leetcode
- 73. Set Matrix Zeroes.
1 parent 5f8c5fb commit e3ee334

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

LeetCode/Easy/Single Number/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
`136`
2+
`Easy`
23

34
# Single Number
45

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
`73`
2+
`Medium`
3+
4+
# Set Matrix Zeroes
5+
6+
Given an `m x n` integer matrix `matrix`, if an element is 0, set its entire row and column to 0's.
7+
8+
You must do it in place.
9+
10+
## Example
11+
12+
!["Matrix 1"](mat_1.png?raw=true "Matrix 1")
13+
```
14+
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
15+
Output: [[1,0,1],[0,0,0],[1,0,1]]
16+
```
17+
18+
## Example
19+
20+
!["Matrix 2"](mat_2.png?raw=true "Matrix 2")
21+
22+
```
23+
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
24+
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
25+
```
26+
27+
## Constraints:
28+
29+
- m == matrix.length
30+
- n == matrix[0].length
31+
- 1 <= m, n <= 200
32+
- 2^31 <= matrix[i][j] <= 2^31 - 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
int row=matrix.length;
4+
int col=matrix[0].length;
5+
int cols=1;
6+
7+
for(int i=0;i<row;i++){
8+
if(matrix[i][0]==0) cols=0;
9+
for(int j=1;j<col;j++){
10+
if(matrix[i][j]==0){
11+
matrix[i][0]=0;
12+
matrix[0][j]=0;
13+
}
14+
}
15+
}
16+
17+
for(int i=row-1;i>=0;i--){
18+
for(int j=col-1;j>=1;j--){
19+
if(matrix[i][0]==0 || matrix[0][j]==0)
20+
matrix[i][j]=0;
21+
}
22+
if(cols==0){
23+
matrix[i][0]=0;
24+
}
25+
}
26+
}
27+
}
22.4 KB
Loading
37.2 KB
Loading

0 commit comments

Comments
 (0)