LeetCode 73 Set Matrix Zeroes
2014-06-18 14:59
483 查看
Given a m 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?
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?
public class Solution { public void setZeroes(int[][] matrix) { int row=matrix.length; int column=matrix[0].length; if(matrix==null||row<1) return ; boolean[] rowflag= new boolean[row]; boolean[] colflag= new boolean[column]; for(int i=0;i<row;i++){ for(int j=0;j<column;j++){ if(matrix[i][j]==0){ rowflag[i]=true; colflag[j]=true; } } } for(int i=0;i<row;i++){ if(rowflag[i]){ for(int j=0;j<column;j++){ matrix[i][j]=0; } } } for(int i=0;i<column;i++){ if(colflag[i]){ for(int j=0;j<row;j++){ matrix[j][i]=0; } } } } }
相关文章推荐
- [Leetcode] 73. Set Matrix Zeroes 解题报告
- [leetcode] 73. Set Matrix Zeroes 解题报告
- 【LeetCode-73】Set Matrix Zeroes
- LeetCode 73 Set Matrix Zeroes
- [LeetCode 73] Set Matrix Zeroes
- [LeetCode-Algorithms-73] "Set Matrix Zeroes" (2017.12.26-WEEK17)
- (java)leetcode-73:Set Matrix Zeroes
- 73 leetcode - Set Matrix Zeroes
- LeetCode73——Set Matrix Zeroes
- leetcode73_ Set Matrix Zeroes
- [leetcode] 73.Set Matrix Zeroes
- [LeetCode] 73. Set Matrix Zeroes 解题思路
- leetcode73 Set Matrix Zeroes
- [LeetCode73]Set Matrix Zeroes
- [leetcode 73] Set Matrix Zeroes
- LeetCode 73 — Set Matrix Zeroes(C++ Java Python)
- Leetcode: 73. Set Matrix Zeroes(Week15, Medium)
- leetcode - 73.Set Matrix Zeroes
- LeetCode(73) Set Matrix Zeroes
- LeetCode(73)Set Matrix Zeroes