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的更多相关文章

  1. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

  2. [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 ...

  3. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 【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. 思路:不能用 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  2. MVC3路由设置访问后缀 html jsp

     C# Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546   publ ...

  3. Cmake设置环境变量

    references: http://stackoverflow.com/questions/21047399/cmake-set-environment-variables-from-a-scrip ...

  4. zabbix如何选择适合的监控类型(107)

    zabbix agent zabbix自带的客户端程序(被动模式),zabbix server主动向它收集监控数据.agent提供丰富的key,包括不限于cpu.内存.网络.磁盘.web等等.如果你不 ...

  5. 2014.7.7 模拟赛【小K的农场】

    3.小K的农场(farm.pas/cpp/c) [题目描述] 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三 ...

  6. 深入了解Json转变为map的思想,附源代码2

    最近在做一个投票情况的用例,返回的结果打算放到JSON中 数据库的结果集如上图所示:optionkey代表选项,optionval代表其值 第一次做的时候考虑应该键值对应的关系,所以前台接到的json ...

  7. Android中各种onTouch事件

    Android里有两个类 android.view.GestureDetector android.view.GestureDetector.SimpleOnGestureListener 1) 新建 ...

  8. C# - List操作 - 按照字母排序

    有Family的类如下: public class FamilyModel { public string Name { set; get; } } 创建List List<FamilyMode ...

  9. mac复制粘贴剪切

    win下复制粘贴剪切: Ctrl+C,Ctrl+V,Ctrl+X; mac下lion之后已经有了一直让win用户吐槽的剪切功能: 复制粘贴剪切:Command+C,Command+V,Command+ ...

  10. UltraEdit 和Notepad++ 使用ftp直接编辑linux上文件

    安装ftp服务 apt-get install vsftpd 安装完后更改相关配置文件 /etc/vsftpd.conf cp /etc/vsftpd.conf /etc/vsftpd.conf.ol ...