leetcode-73 set matrix zeros
来源:互联网 发布:淘宝充话费赚钱吗 编辑:程序博客网 时间:2024/06/09 16:11
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
题意是将矩阵中,有0的行列值均置为0.
这道题想要原址,首先需要记录下首行、首列,是否有0。其次从2行2列开始统计,如果有0,则将此位置的首行、首列位置记为0,实现原址。
在改的时候应当注意,首行首列的值需要最后更改。
感谢博主 http://blog.csdn.net/ljiabin/article/details/40423045
附上代码
class Solution {public:void setZeroes(vector<vector<int>>& matrix){if (matrix.empty())return;int row = matrix.size();int col = matrix[0].size();bool firstrow_has_zero = false;bool firstcol_has_zero = false;//判断首行首列有没有0for (int j = 0; j < col; j++){if (matrix[0][j] == 0){firstrow_has_zero = true;break;}}for (int i = 0; i < row; i++){if (matrix[i][0] == 0){firstcol_has_zero = true;break;}}//从2行2列开始标记,是否有0for (int i = 1; i<row; i++){for (int j = 1; j<col; j++){if (matrix[i][j] == 0){matrix[0][j] = 0;matrix[i][0] = 0;}}}//第一列为0的判断for (int i = 1; i<row; i++){if (matrix[i][0] == 0){for (int j = 1; j<col; j++){matrix[i][j] = 0;}}}//第一行为0的判断for (int j = 1; j<col; j++){if (matrix[0][j] == 0){for (int i = 1; i<row; i++){matrix[i][j] = 0;}}}if (firstcol_has_zero){for (int i = 0; i < row; i++){matrix[i][0] = 0;}}if (firstrow_has_zero){for (int j = 0; j < col; j++){matrix[0][j] = 0;}}return;}};
0 0
- LeetCode 73:Set Matrix Zeros
- leetcode-73 set matrix zeros
- [LeetCode] Set Matrix Zeros
- [Leetcode] Set Matrix Zeros
- 【Leetcode】Set matrix zeros
- LeetCode - Set Matrix Zeros
- LeetCode---set-matrix-zeros
- [LeetCode] Set Matrix Zeros
- LeetCode--Set matrix zeros
- 73-Set Matrix Zeros
- LeetCode - 73. Set Matrix Zeros
- leetcode 73 Set Matrix Zeros 矩阵置零
- LeetCode 题解(21): Set Matrix Zeros
- LeetCode刷题(39)--Set Matrix Zeros
- Set Matrix Zeros
- set matrix zeros
- set matrix zeros
- leetcode_073 Set Matrix Zeros
- leetcode题解-64. Minimum Path Sum
- 前端面试题总结
- 51nod 2级算法题-1092
- 享元模式
- 找不到类型或命名空间名称“type/namespace”(是否缺少 using 指令或程序集引用?)
- leetcode-73 set matrix zeros
- Java AOP
- java中单线程和多线程
- (译)LearnOpenGL实际案例Breakout(四):关卡
- RabbitMQ交换机的理解
- 关于一些 java初级的问题
- JavaScript闭包详解
- LBS定位技术之GPS
- conversion constructor