File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn set_zeroes ( matrix : & mut Vec < Vec < i32 > > ) {
3
+ let m = matrix. len ( ) ;
4
+ let n = matrix. first ( ) . unwrap ( ) . len ( ) ;
5
+ let erased_row_0 = ( 0 ..n) . any ( |i| matrix[ 0 ] [ i] == 0 ) ;
6
+ let erased_col_0 = ( 0 ..m) . any ( |i| matrix[ i] [ 0 ] == 0 ) ;
7
+ for i in 1 ..m {
8
+ for j in 1 ..n {
9
+ if matrix[ i] [ j] == 0 {
10
+ * matrix[ i] . first_mut ( ) . unwrap ( ) = 0 ;
11
+ matrix. first_mut ( ) . unwrap ( ) [ j] = 0 ;
12
+ }
13
+ }
14
+ }
15
+ for i in 1 ..m {
16
+ if * matrix[ i] . first ( ) . unwrap ( ) != 0 {
17
+ continue ;
18
+ }
19
+ for j in 0 ..n {
20
+ matrix[ i] [ j] = 0 ;
21
+ }
22
+ }
23
+ for j in 1 ..n {
24
+ if matrix. first ( ) . unwrap ( ) [ j] != 0 {
25
+ continue ;
26
+ }
27
+ for i in 0 ..m {
28
+ matrix[ i] [ j] = 0 ;
29
+ }
30
+ }
31
+ if erased_row_0 {
32
+ for j in 0 ..n {
33
+ matrix[ 0 ] [ j] = 0 ;
34
+ }
35
+ }
36
+ if erased_col_0 {
37
+ for i in 0 ..m {
38
+ matrix[ i] [ 0 ] = 0 ;
39
+ }
40
+ }
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments