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 it in place.
思路:
如果允许额外存储行和列清零的信息,那么题目就没有意思了。希望只用O(1)的存储空间的时候,我们就得利用矩阵本身的存储空间了:把清零的信息保存在第一行和第一列里。
#include<iostream>
#include<vector>
using namespace std;
int main()
{vector<vector<int>> m = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };int row = m.size();int col = m[0].size();bool frow = false;bool fcol = false;for (int i = 0; i < col; i++){if (m[0][i] == 0)frow = true;}for (int i = 0; i < row; i++){if (m[i][0] == 0)fcol = true;}//对于矩阵中的其他元素,标志所在的行和列进行标注为0 这是在第一行的元素上进行的for (int i = 1; i < row; i++){for (int j = 1; j < col; j++){if (m[i][j] == 0){m[i][0] = 0;m[0][j] = 0;}}}//现在对第一行中标注为0的行进行全部为0/*for (int i = 1; i < row; i++){if (m[i][0]==0)for (int j = 1; j < col; j++)m[i][j] == 0;}for (int i = 1; i < col; i++){if (m[0][i] == 0)for (int j = 1; j < row; j++)m[j][i] == 0;}*/for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (m[i][0] == 0 || m[0][j] == 0)m[i][j] == 0;}}//判断第一行元素if (frow==true)for (int i = 0; i < col; i++)m[0][i] == 0;if (fcol == true){for (int i = 0; i < row; i++)m[i][0] == true;}
}
Set Matrix Zeroes相关推荐
- 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) ...
- 【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. 思路:不能用 ...
- 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 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. 题意: ...
- 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 (73): Set Matrix Zeroes
链接: https://leetcode.com/problems/set-matrix-zeroes/ [描述] Given a m x n matrix, if an element is 0, ...
- leetcode解题文件夹
点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
最新文章
- python 之 分割参数getopt
- LeetCode-链表-面试题 02.07. 链表相交
- python爬取数据存入mysql
- hdu 3746 Cyclic Nacklace
- linux后台运行命令,nohup
- CSS3选择器(二)--表单
- 工作200:视频上传和图片编辑功能
- tomcat的jdbc连接池PoolExhaustedException 1
- org.springframework.hateoas.mvc.ControllerLinkBuilder之ClassNotFoundException的错误
- 【Hive的高级查询详】
- 信息流媒体变现_抖音信息流投放优势,为什么选择抖音信息流广告投放呢?
- mysql分组聚合compute_SQLServer之Compute/ComputeBy实现数据汇总
- 新机Switch OLED真机实测分享
- C语言经典实例006:输出一个正方形
- linux 开启ssdp服务,无法网络发现,Windows Server 2012如何启用SSDP Discovery服务
- 视频变声音的软件有哪些?剪辑视频怎么改变声音
- 开发工作流程_您应该了解的9个开发工作流程升级
- ES9023P音频DAC解码芯片
- 【AE软件】视频添加字幕
- 201771010101 白玛次仁 《2018面向对象程序设计(Java)》第十七周学习总结