leetcode -- Set Matrix Zeroes
2013-08-20 10:46
423 查看
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?
[解题思路]
1.空间复杂度为:O(m+n)
2.constant space solution
常数空间,第一可以考虑是不是固定数量的几个变量能解决,否则可以考虑是不是问题本身已经提供了足够的空间
本题属于后者,利用矩阵的第一行和第一列来作辅助空间使用
1.先确定第一行和第一列是否需要清零
即,看看第一行中是否有0,记下来。也同时记下来第一列中有没有0。
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的。
3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了
即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
4.根据1中确定的状态,处理第一行和第一列。
如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。
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?
[解题思路]
1.空间复杂度为:O(m+n)
public void setZeroes(int[][] matrix) { // Start typing your Java solution below // DO NOT write main() function if(matrix.length == 0){ return; } int r = matrix.length; int c = matrix[0].length; int[] rows = new int[r]; for(int i = 0; i < r; i++){ rows[i] = 1; } int[] cols = new int[c]; for(int i = 0; i < c; i++){ cols[i] = 1; } for(int i = 0; i < r; i++){ for(int j = 0; j < c; j++){ if(matrix[i][j] == 0){ rows[i] = 0; cols[j] = 0; } } } for(int i = 0; i < r; i++){ if(rows[i] == 0){ for(int j = 0; j < c; j++){ matrix[i][j] = 0; } } } for(int i = 0; i < c; i++){ if(cols[i] == 0){ for(int j = 0; j < r; j++){ matrix[j][i] = 0; } } } }
2.constant space solution
常数空间,第一可以考虑是不是固定数量的几个变量能解决,否则可以考虑是不是问题本身已经提供了足够的空间
本题属于后者,利用矩阵的第一行和第一列来作辅助空间使用
1.先确定第一行和第一列是否需要清零
即,看看第一行中是否有0,记下来。也同时记下来第一列中有没有0。
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的。
3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了
即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
4.根据1中确定的状态,处理第一行和第一列。
如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。
public void setZeroes(int[][] matrix) { // Start typing your Java solution below // DO NOT write main() function if(matrix.length == 0){ return; } int r = matrix.length; int c = matrix[0].length; boolean zeroRow = false, zeroCol = false; for(int i = 0; i < c; i++){ if(matrix[0][i] == 0){ zeroRow = true; break; } } for(int i = 0; i < r; i++){ if(matrix[i][0] == 0){ zeroCol = true; break; } } for(int i = 1; i < r; i++){ for(int j = 1; j < c; j++){ if(matrix[i][j] == 0){ matrix[i][0] = 0; matrix[0][j] = 0; } } } for(int i = 1; i < r;i ++){ for(int j = 1; j < c; j++){ if(matrix[i][0] == 0 || matrix[0][j] == 0){ matrix[i][j] = 0; } } } if(zeroRow){ for(int i = 0; i < c; i++){ matrix[0][i] = 0; } } if(zeroCol){ for(int i = 0; i < r; i++){ matrix[i][0] = 0; } } }
相关文章推荐
- LeetCode-Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- [Leetcode] 73. Set Matrix Zeroes 解题报告
- LeetCode:Set Matrix Zeroes
- [leetcode]Set Matrix Zeroes @ Python
- LeetCode-Set Matrix Zeroes
- LeetCode --- 73. Set Matrix Zeroes
- leetcode73 Set Matrix Zeroes
- LeetCode | Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- leetcode Set Matrix Zeroes 代码解析
- LeetCode之Set Matrix Zeroes
- LeetCode – Set Matrix Zeroes (Java)
- Leetcode Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- 【LeetCode】C# 73、Set Matrix Zeroes
- 【一天一道LeetCode】#73. Set Matrix Zeroes
- 【leetcode刷题笔记】Set Matrix Zeroes
- LeetCode:Set Matrix Zeroes