73. Set Matrix Zeroes
2016-04-30 23:57
183 查看
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.
Subscribe to see which companies asked this question
这题目关键是要求O(1)的空间。
可以使用第1行,第1列来保存数据信息。
click to show follow up.
Subscribe to see which companies asked this question
这题目关键是要求O(1)的空间。
可以使用第1行,第1列来保存数据信息。
class Solution { public: void setZeroes(vector<vector<int> > &matrix) { bool firstrow = false; bool firstcol = false; for(int j = 0; j < col; ++j) if(matrix[0][j] == 0){ firstrow = true; break; } for(int i = 0; i < row; ++i) if(matrix[i][0] == 0){ firstcol = true; break; } 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) for(int j = 1; j < col; ++j) if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; if(firstrow){ for(int j = 0; j < col; ++j) matrix[0][j] = 0; } if(firstcol){ for(int i = 0; i < row; ++i) matrix[i][0] = 0; } } };
相关文章推荐
- Linux.fedora23.WORKSTATION简易安装教程(WIN10+Linux双系统)
- C++中的 Round(),floor(),ceil()
- 请编写一个算法,若MxN矩阵中某个元素为0,则将其所在的行与列清零。 给定一个MxN的int[][]矩阵(C++中为vector>)mat和矩阵的阶数n,请返回完成操作后的int[][]矩阵(C++中
- Servlet初识
- LUA OOP编程实现方法
- spring boot 集成springmvc视图技术
- Unity Official Tutorial OF PICKING UP COLLECTABLES --- Camera Follows Player
- 有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度。
- 逆波兰计算器与中缀表达式向后缀表达式的转化实例
- HDU_2295_Radar(DancingLinksX重复覆盖+二分)
- hiho第九十六周 数论五·欧拉函数
- js中的$和#
- eclipse中的server location灰色如何修改?
- 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。
- ubuntu14.04 Nvidia 驱动和cuda安装(转)
- VO ,PO ,BO,QO, DAO ,POJO,概念
- HTML特殊编码转换
- 网站添加第三方登陆(PHP版)
- J2ee简介
- iOS9 新增特性(汇)