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 ...
随机推荐
- Awesome Go
A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...
- hdu2769:枚举+同余方程
题意:有一个随机数生成器 x[i+1]=(a*x[i]+b)%10001 已知 x1,x3,x5...求 x2,x4,x6...... x的个数为 2n (n<=10000) a,b也在 0 ...
- 股票市场问题(The Stock Market Problem)
Question: Let us suppose we have an array whose ith element gives the price of a share on the day i. ...
- [深入JUnit] 为什么别测试private函数
[深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...
- day57:00:26:34
今天开始用博客记录倒计时,也只是为了看看今天做了什么.这也是我第一用博客园记录考研生活了 倒计时57天,我在想每天花时间在这记录生活会不会浪费复习的时间,其实不会的了,不去看微博,少刷新闻....仔细 ...
- js原型和原型链个人理解(我的理解)
<script> //普通对象与函数对象,js万物皆是对象 //自带的 function a1() { function f1() {} var f2=function () {} var ...
- AIX下RAC搭建 Oracle10G(一)检測系统环境
AIX下RAC搭建系列 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX203 AIX204 交换机 SAN光纤交换机 存储 S ...
- Cuts the cake_hdu_2134.java
Cuts the cake Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- [Node.js] Use "prestart" in scripts
Usually we run : npm start to start an app, then we we might call other script to do something: npm ...
- [置顶] ./build_native 时出现please define NDK_ROOT
在一次帮朋友弄cygwin交叉编译时出现了这个问题 cygwin是按照成功了,make-v,以及gcc-v都没出现问题,就是在./build_native 时出现please define NDK_R ...