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.
很挫的一个想法: 使用O(M+N)的space
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[0].size();
if(m < 1 || n < 1) return ;
bool *row = new bool[m];
bool *column = new bool[n];
memset(row, 0, sizeof(bool)*m);
memset(column, 0, sizeof(bool)*n);
for(int i = 0; i< m; i++)
for(int j = 0; j< n ; j++)
if(matrix[i][j] == 0){
row[i] = true;
column[j] = true;
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(row[i]||column[j])
matrix[i][j] = 0;
delete []row;
delete []column;
}
};
比较好的一个思路:常数空间的话,第一可以考虑是不是固定数量的几个变量能搞定;否则可以考虑是不是问题本身已经提供了足够的空间。
这道题属于后者,就是利用矩阵的第一行和第一列来作为辅助空间使用。不用开辟新的存储空间。方法就是:
1.先确定第一行和第一列是否需要清零
即,看看第一行中是否有0,记下来。也同时记下来第一列中有没有0。
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0
这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的。
3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了
即,拿第一行为例,如果扫描到一个0,就将这一列都清0.
4.根据1中确定的状态,处理第一行和第一列。
如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
int n = matrix[].size();
if(m < || n < ) return ;
bool zeroR = false, zeroC = false;
//set zeroR
for(int i = ; i< n ; i++)
if(matrix[][i] == ){
zeroR = true;
break;
}
//set zeroC
for(int i = ; i < m; i++)
if(matrix[i][] == ){
zeroC = true;
break;
}
//scan others
for(int i = ; i< m; i++)
for(int j = ; j< n ; j++)
if(matrix[i][j] == ){
matrix[i][] = ;
matrix[][j] = ;
}
// set others
for(int i = ; i < m; i++)
for(int j = ; j < n; j++)
if(matrix[i][] == ||matrix[][j] == )
matrix[i][j] = ;
//set zero row
if(zeroR){
for(int i = ; i< n;i++)matrix[][i] = ;
}
//set zero clumn
if(zeroC){
for(int i = ; i< m ;i++) matrix[i][] = ;
}
}
};
LeetCode_Set Matrix Zeroes的更多相关文章
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- Leetcode 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- LeetCode: Set Matrix Zeroes 解题报告
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- 【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 java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fol ...
随机推荐
- VMware Network Adapter VMnet1和VMnet8 未识别的网络的解决方法
VMware Network Adapter VMnet1和VMnet8 被防火墙认定为未识别的网络,阻隔,无法使用端口映射,虚拟机的80端口无法传入,数据包只能出不能入.且公用网络被限制不能修改为家 ...
- 【转】Android API 中文(14) —— ViewStub
用一个XML源填充view.inflate(上下文对象,资源文件Id,父窗口组一般为null): 原文网址:http://www.cnblogs.com/over140/archive/2010/10 ...
- zoj2562:搜索+数论(反素数)
题目大意:求n以内因子数量最多的数 n的范围为1e16 其实相当于求n以内最大的反素数... 由素数中的 算数基本原理 设d(a)为a的正因子的个数,则 d(n)=(a1+1)(a2+1)..... ...
- 解决 在IE与firefox宽度不一致的问题
浏览器默认不同的字体问题,字体分为“等宽”和“不等宽”字体,所以 在IE与firefox内间距是不等的. 解决办法: body{font-family: 宋体, simsun; ...
- 解决IE6下DIV无法实现1px高度问题
2.多加一个line-height:1px的属性,不过得在DIV里多加一个 ,也就是空格,以下为引用的内容: <styletypestyletype="text/css"&g ...
- MVC MVC 路由详解
在项目中我们引用了System.Web.Routing; Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...
- log4net 快速上手使用
*本随笔仅限快速上手,如需深入探究,可查阅其它博友. 一.下载log4net.dll并添加引用; 二.添加配置文件 log4net.xml : <?xml version="1.0&q ...
- css中的段落样式及背景
一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...
- [RxJS] Basic DOM Rendering with Subscribe
While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...
- Network 20Q--Q2 How does Google sell ad spaces?
在使用Google搜索的时候会发现,搜索出来的页面除了在左边显示搜索结果以外,还会页面的右边推荐一些广告.那么Google是怎么从这些广告挣钱以及广告商可以通过Google广告获得什么利益呢? Goo ...