set-matrix-zeroes
来源:互联网 发布:win10有网络不能上网 编辑:程序博客网 时间:2024/03/29 19:06
给定一个m×n矩阵,如果一个元素是0,则将其所在行和列全部元素变成0。
需要在原地完成。
您在真实的面试中是否遇到过这个题?Yes
样例
给出一个矩阵[[1,2],[0,3]],返回[[0,2],[0,0]]
不使用额外空间
class Solution {public: /** * @param matrix: A list of lists of integers * @return: Void */ void setZeroes(vector<vector<int> > &matrix) { // write your code here int row=matrix.size(); if(row==0) return; int col=matrix[0].size(); if(col==0) return; bool firstcolhaszero=false; for(int i=0;i<row;i++){ if(matrix[i][0]==0) firstcolhaszero=true; } bool firstrowhaszero=false;; for(int i=0;i<col;i++){ if(matrix[0][i]==0) firstrowhaszero=true; } for(int i=1;i<row;i++){ for(int j=1;j<col;j++){ if(matrix[i][j]==0){ matrix[i][0]=0; matrix[0][j]=0; } } } for(int i=1;i<row;i++){ if(matrix[i][0]==0){ for(int j=1;j<col;j++) matrix[i][j]=0; } } for(int i=1;i<col;i++){ if(matrix[0][i]==0){ for(int j=1;j<row;j++) matrix[j][i]=0; } } if(firstcolhaszero){ for(int i=0;i<row;i++){ matrix[i][0]=0; } } if(firstrowhaszero){ for(int i=0;i<col;i++){ matrix[0][i]=0; } } }};
0 0
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- [Leetcode] Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- [LeetCode] Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [leetcode]Set Matrix Zeroes
- Leetcode:Set Matrix Zeroes
- Set Matrix Zeroes
- Class文件结构
- 简单的网页验证码设计——学习笔记
- 什么是XPCOM
- PowerShell Function之获取OS信息
- 关于编码
- set-matrix-zeroes
- 调用Android相机拍照的问题
- 剖析怎样使用Spring的PropertyPlaceholderConfigurer占位符
- 如何查找一个变量在哪里被调用了
- C语言版Trim()函数,去除字符串中的空字符
- 警惕UNIX下的LD_PRELOAD环境变量
- UEFI下装Ubuntu Win10 双系统
- <仅是自己做笔记。。。系列-11>输入n,用最快的方法求Fibonacci数列的第n项。
- 外键值可为空