73. Set Matrix Zeroes (Matrix)

O(m + n)space 简单的 row和col分别设置标志

 

 1 class Solution {
 2     public void setZeroes(int[][] matrix) {
 3         int[] row = new int[matrix.length];
 4         int[] col = new int[matrix[0].length];
 5         for(int i = 0; i < matrix.length; i++) {
 6             for(int j = 0; j < matrix[0].length; j++) {
 7                 if(matrix[i][j] == 0) {
 8                     row[i] = 1;
 9                     col[j] = 1;
10                 }
11             }
12         }
13         
14         for(int i = 0; i < matrix.length; i++) {
15             if(row[i] == 1) {
16                 for(int j = 0; j < matrix[0].length; j++) {
17                     matrix[i][j] = 0;
18                 }
19             }
20         }
21         
22         for(int j = 0; j < matrix[0].length; j++) {
23             if(col[j] == 1) {
24                 for(int i = 0; i < matrix.length; i++) {
25                     matrix[i][j] = 0;
26                 }
27             }
28         }
29     }
30 }

 

posted @ 2018-08-27 05:24  jasoncool1  阅读(111)  评论(0编辑  收藏  举报