set-matrix-zeroes当元素为0则设矩阵内行与列均为0
题目描述
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.
Did you use extra space?
A straight forward solution using O(m n) 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 class Solution { 2 public: 3 void setZeroes(vector<vector<int> > &matrix) { 4 vector<bool> col,row; 5 col.resize(matrix[0].size(), false); 6 row.resize(matrix.size(), false); 7 for (int i = 0; i < matrix.size(); ++i) 8 { 9 for (int j = 0; j < matrix[i].size();++j) 10 { 11 if(matrix[i][j]==0) 12 { 13 col[j]=true; 14 row[i]=true; 15 } 16 } 17 } 18 for (int i = 0; i < matrix.size(); ++i) 19 { 20 for (int j = 0; j < matrix[i].size();++j) 21 { 22 if(col[j]||row[i]) 23 { 24 matrix[i][j]=0; 25 } 26 } 27 } 28 } 29 };
最优解法:
首先判断第一行和第一列是否有元素为0,而后利用第一行和第一列保存状态,最后根据开始的判断决定是否将第一行和第一列置0
1 //时间复杂度O(mn),空间复杂度O(1) 2 //利用第一行和第一列的空间做记录 3 class Solution { 4 public: 5 void setZeroes(vector<vector<int> > &matrix) { 6 const int row = matrix.size(); 7 const int col = matrix[0].size(); 8 bool row_flg = false, col_flg = false; 9 10 //判断第一行和第一列是否有零,防止被覆盖 11 for (int i = 0; i < row; i++) 12 if (0 == matrix[i][0]) { 13 col_flg = true; 14 break; 15 } 16 for (int i = 0; i < col; i++) 17 if (0 == matrix[0][i]) { 18 row_flg = true; 19 break; 20 } 21 //遍历矩阵,用第一行和第一列记录0的位置 22 for (int i = 1; i < row; i++) 23 for (int j = 1; j < col; j++) 24 if (0 == matrix[i][j]) { 25 matrix[i][0] = 0; 26 matrix[0][j] = 0; 27 } 28 //根据记录清零 29 for (int i = 1; i < row; i++) 30 for (int j = 1; j < col; j++) 31 if (0 == matrix[i][0] || 0 == matrix[0][j]) 32 matrix[i][j] = 0; 33 //最后处理第一行 34 if (row_flg) 35 for (int i = 0; i < col; i++) 36 matrix[0][i] = 0; 37 if (col_flg) 38 for (int i = 0; i < row; i++) 39 matrix[i][0] = 0; 40 } 41 };
转载于:https://www.cnblogs.com/zl1991/p/9630343.html
set-matrix-zeroes当元素为0则设矩阵内行与列均为0相关推荐
- Set Matrix Zeroes
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
- Set Matrix Zeroes leetcode
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
- LeetCode-73. Set Matrix Zeroes
原题描述 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. F ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- LeetCode 73. Set Matrix Zeroes
LeetCode 73. Set Matrix Zeroes Solution1:我的答案 比较笨,算法时间复杂度是O(mn)O(mn)O(mn),占用额外空间是O(m+n)O(m+n)O(m+n) ...
- 有一个5 * 5的二维数组,保留主对角线上的元素,并使其他元素均为0,要求用函数和子函数完成
<程序设计基础实训指导教程-c语言> ISBN 978-7-03-032846-5 p143 7.1.2 上级实训内容 [实现内容8]有一个5 * 5的二维数组,保留主对角线上的元素,并使 ...
- LeetCode (73): Set Matrix Zeroes
链接: https://leetcode.com/problems/set-matrix-zeroes/ [描述] Given a m x n matrix, if an element is 0, ...
- 三数之和给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组(GO,PHP)
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组. 注意:答案中不可以包含重复的三 ...
- 补充函数编程,程序功能要求:有一个已知数组d,程序调用函数out输出数组 d的所有元素;调用函数plus对数组d中所有大于0的元素分别加上30;调用函数 cpy将d数组所有元素
void out(int *p,int n) { int i=0; //1分 for(i=0;i< ...
最新文章
- AAAI2020录用论文汇总(二)
- 深度网络pre-train对于深度网络的意义
- linux编译ffmpeg成so,「ffmpeg」一 mac 环境下编译ffmpeg,生成so库文件
- python post与get的区别_Python Requests库 Get和Post的区别和Http常见状态码
- element tree不刷新视图_随手“一片”SCI,Qiime2扩增子处理流程确定不了解一下?(一)...
- C++深度探索系列:智能指针(Smart Pointer) [一] (转)
- linux网络编程学习笔记之四 -----多-threaded服务器
- vc6.0怎么新建c语言项目
- linux c正则
- 各省研究与试验发展(R&D)人员全时当量(1998-2018年)
- 大连民族大学计算机科学学院,魏巍 - 大连民族大学 - 计算机科学与工程学院
- 腾讯云主机SSH连接不上如何解决
- Java Web中乱码问题
- 一男老师每日百词转载+连载(1)
- 《Linux性能优化实战》笔记(十三)—— 如何“快准狠”找到系统内存的问题
- DD每周前端七题详解-第五期
- FHQ_TREAP学习笔记
- 藏宝阁游戏服务器维护中,梦幻西游2013年1月22日藏宝阁维护公告 17173.com网络游戏:《梦幻西游》专区...
- 微型计算机接口与技术的交通灯,微机原理与接口技术课程设计——交通灯设计.doc...
- 《C++ 黑客编程揭秘与防范(第2版)》—第6章6.2节详解PE文件结构