73. Set Matrix Zeroes
来源:互联网 发布:淘宝网店开店要多少钱 编辑:程序博客网 时间:2024/06/17 21:34
/*Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.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)的空间复杂度,只需两个状态数组(长度分别为m、n)分别存储行列的状态即可,然后根据状态数组处理矩阵即可。2.如何用O(1)的空间复杂度来处理,还需要记录行列的状态。看到有人采用矩阵本身的一行、一列 来作为状态数组,节约了空间。*/class Solution {public: void setZeroes(vector<vector<int>>& matrix) { if(matrix.empty()) return ;int row=matrix.size(),column=matrix[0].size();//以第一行与第一列作为状态数组bool row_first=false,column_first=false;for(int i=0;i<column;i++)//第一行状态if(matrix[0][i]==0){row_first=true;break;}for(int i=0;i<row;i++)//第一列状态if(matrix[i][0]==0){column_first=true;break;}for(int i=1;i<row;i++)//状态数组for(int j=1;j<column;j++)if(matrix[i][j]==0)matrix[0][j]=0,matrix[i][0]=0; for(int i=1;i<row;i++)//按行处理for(int j=1;matrix[i][0]==0 && j<column;j++)matrix[i][j]=0;for(int j=1;j<column;j++)//按列处理for(int i=1;matrix[0][j]==0 && i<row;i++)matrix[i][j]=0;if(row_first)for(int i=0;i<column;i++)matrix[0][i]=0;if(column_first)for(int i=0;i<row;i++)matrix[i][0]=0; }};
阅读全文
0 0
- 73.Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 输入网址到得到网页响应全过程
- 通过apktool和逆向助手反编译apk
- Linux 常用命令
- "application/vnd.android.package-archive"是什么?
- 《统计学习方法》 决策树 CART生成算法 回归树 Python实现
- 73. Set Matrix Zeroes
- 创建型-抽象工厂模式
- Ⅳ vue2.0 项目中的组件
- 第一篇博客
- Android:screenOrientation属性
- 单例模式
- 多线程的两种实现方式和区别?
- java 显式域初始化
- ajax获取当前城市天气情况