Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
Solution:
非常无聊的一道题。解题点就在于清空标志位存在哪里的问题。可以创建O(m+n)的数组来存储,但此题是希望复用已有资源。这里可以选择第一行和第一列来存储标志位。
所以使用constant space 其实是指复用空间而已。
1.先确定第一行和第一列是否需要清零
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
3.根据第一行和第一列的信息,已经可以讲剩下的矩阵元素赋值为结果所需的值了
4.根据1中确定的状态,处理第一行和第一列。
1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 int row = 0; 6 int col = 0; 7 int sig = 0; 8 if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return; 9 for(int i = 0; i < matrix.length; i ++){ 10 for(int j = 0; j < matrix[0].length; j ++){ 11 if(matrix[i][j] == 0){ 12 if(sig == 0){ 13 row = i; 14 col = j; 15 sig = 1; 16 }else{ 17 matrix[row][j] = 0; 18 matrix[i][col] = 0; 19 } 20 } 21 } 22 } 23 if(sig == 1){ 24 for(int i = 0; i < matrix.length; i ++){ 25 for(int j = 0; j < matrix[0].length; j ++){ 26 if(matrix[i][col] == 0 || matrix[row][j] == 0){ 27 matrix[i][j] = 0; 28 } 29 } 30 } 31 } 32 } 33 }
网上的解法:
1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 4 boolean firstRow=false, firstColumn=false; 5 for(int i=0;i<matrix.length;i++){ 6 if(matrix[i][0]==0){ 7 firstColumn = true; 8 break; 9 } 10 } 11 12 for(int i=0;i<matrix[0].length;i++){ 13 if(matrix[0][i]==0){ 14 firstRow = true; 15 break; 16 } 17 } 18 19 for(int i=1;i<matrix.length;i++){ 20 for(int j=1;j<matrix[0].length;j++){ 21 if(matrix[i][j]==0){ 22 matrix[i][0]=0; 23 matrix[0][j]=0; 24 } 25 } 26 } 27 28 for(int i=1;i<matrix.length;i++){ 29 for(int j=1;j<matrix[0].length;j++){ 30 if(matrix[i][0]==0||matrix[0][j]==0){ 31 matrix[i][j]=0; 32 } 33 } 34 } 35 36 if(firstRow){ 37 for(int i=0;i<matrix[0].length;i++) 38 matrix[0][i]=0; 39 } 40 41 if(firstColumn){ 42 for(int i=0;i<matrix.length;i++) 43 matrix[i][0]=0; 44 } 45 46 } 47 }
第二遍:
找到哪些地方要设为0很容易,但是注意当变0的时候一定要注意顺序,不要后面又check之前改过的值,这样会把全部都变成0。
1 public class Solution { 2 public void setZeroes(int[][] matrix) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 int row = 0; 6 int col = 0; 7 int sig = 0; 8 if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return; 9 for(int i = 0; i < matrix.length; i ++) 10 for(int j = 0; j < matrix[0].length; j ++) 11 if(matrix[i][j] == 0){ 12 if(sig == 0){ 13 row = i; 14 col = j; 15 sig = 1; 16 }else{ 17 matrix[row][j] = 0; 18 matrix[i][col] = 0; 19 } 20 } 21 if(sig == 1){ 22 for(int i = 0; i < matrix.length; i ++) 23 if(matrix[i][col] == 0 && i != row) 24 for(int j = 0; j < matrix[0].length; j ++) 25 matrix[i][j] = 0; 26 for(int i = 0; i < matrix[0].length; i ++) 27 if(matrix[row][i] == 0) 28 for(int j = 0; j < matrix.length; j ++) 29 matrix[j][i] = 0; 30 for(int j = 0; j < matrix[0].length; j ++) 31 matrix[row][j] = 0; 32 } 33 } 34 }