2.1.20—线性表—Set Matrix Zeroes
来源:互联网 发布:sql语法 编辑:程序博客网 时间:2024/06/05 13:29
描述
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.
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?
#include<iostream>#include<vector>#include<string>using namespace std;const int n = 5;void SetMatrixZeros(int a[][n], int m){bool rowflag = false;bool colflag = false;for (int i = 0; i < n; i++){if (a[0][i] == 0){rowflag = true;break;}}for (int i = 0; i < m; i++){if (a[i][0] == 0){colflag = true;break;}}//for (int i = 1; i < m; i++){for (int j = 1; j < n; j++){if (a[i][j] == 0){a[i][0] = 0;a[0][j] = 0;}}}//for (int i = 1; i < m; i++){for (int j = 1; j < n; j++){if (a[i][0] == 0 || a[0][j] == 0)a[i][j] = 0;}}//if (rowflag){for (int i = 0; i < n; i++){a[0][i] = 0;}}if (colflag){for (int j = 0; j < m; j++){a[j][0] = 0;}}}int main(){const int m = 4;int a[m][n] = { { 0, 2, 3, 4, 5 }, { 3, 7, 8, 9, 0 }, { 0, 1, 2, 3, 4 }, { 1, 5, 6, 0, 9 } };SetMatrixZeros(a, m);for (int i = 0; i < m; i++){for (int j = 0; j < n; j++)cout << a[i][j] << " ";cout << endl;}}
阅读全文
0 0
- 2.1.20—线性表—Set Matrix Zeroes
- 2.1.20 Set Matrix Zeroes
- 20 : Set Matrix Zeroes
- LeetCode —— Set Matrix Zeroes
- LeetCode——Set Matrix Zeroes
- LeetCode——Set Matrix Zeroes
- leetcode 073 —— Set Matrix Zeroes
- LeetCode73——Set Matrix Zeroes
- Leetcode题集——set-matrix-zeroes
- 算法系列——Set Matrix Zeroes
- LeetCode——73. Set Matrix Zeroes
- Algorithms—73.Set Matrix Zeroes
- 2.1.19 Set Matrix Zeroes
- leetcode——73——Set Matrix Zeroes
- leetcode系列题目——Set Matrix Zeroes
- 【leetcode】Array—— Set Matrix Zeroes(73)
- LeetCode 73 — Set Matrix Zeroes(C++ Java Python)
- LeetCode 之 Set Matrix Zeroes — C++实现
- 试一试啊
- SAP-FICO-AR-关于剩余支付和部分支付的区别
- JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer
- JS的简介
- hdu 1406 完数 筛法求因子和
- 2.1.20—线性表—Set Matrix Zeroes
- Two Sum II
- JAVA的三种任务调度,Timmer,ScheduledExecutorService和Quartz
- linux命令
- Xcode统计代码行数
- HDU 2822 Dogs(优先队列)
- golang中并发sync和channel
- 148 Sort List (归并实现链表排序)
- springmvc过滤静态资源的配置