File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ Given a * m* x * n* matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ class Index {
3+ public final int first ;
4+ public final int second ;
5+ public Index (int first , int second ) {
6+ this .first = first ;
7+ this .second = second ;
8+ }
9+ }
10+
11+ public void setZeroes (int [][] matrix ) {
12+ if (matrix == null || matrix .length == 0 || matrix [0 ].length == 0 )
13+ return ;
14+
15+ List <Index > indexList = new ArrayList <Index >();
16+ for (int i = 0 ; i < matrix .length ; i ++) {
17+ for (int j = 0 ; j < matrix [i ].length ; j ++) {
18+ if (matrix [i ][j ] == 0 )
19+ indexList .add (new Index (i , j ));
20+ }
21+ }
22+
23+ for (Index idx : indexList ) {
24+ for (int i = 0 ; i < matrix [0 ].length ; i ++)
25+ matrix [idx .first ][i ] = 0 ;
26+
27+ for (int i = 0 ; i < matrix .length ; i ++)
28+ matrix [i ][idx .second ] = 0 ;
29+ }
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments