73. Set Matrix Zeroes (Array)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
思路:第一行和第一列用来标示该行、该列是否全0,但事先得判断第一行、第一列是否全0=>用两个额外的变量存储
class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty()) return;
        bool firstLineZero = false;
        bool firstColumnZero = false;
        if(matrix[][]==){
            firstLineZero = true;
            firstColumnZero = true;
        }
        //the first line
        for(int i = ; i<matrix[].size(); i++)
        {
            if(matrix[][i]!=) continue;
            firstLineZero = true;
            break;
        }
         //the first column
        for(int i = ; i<matrix.size(); i++)
        {
            if(matrix[i][]!=) continue;
            firstColumnZero = true;
            break;
        }
        for(int i = ; i < matrix.size(); i++)
        {
            for(int j = ; j<matrix[].size(); j++)
            {
                if(matrix[i][j] != ) continue;
                matrix[i][] = ;
                matrix[][j] = ;
            }
        }
        for(int i = ; i<matrix[].size(); i++)
        {
            if(matrix[][i]!=) continue;
            for(int j = ; j<matrix.size(); j++)
            {
                matrix[j][i]=;
            }
        }
        for(int i = ; i<matrix.size(); i++)
        {
            if(matrix[i][]!=) continue;
            for(int j = ; j<matrix[].size(); j++)
            {
                matrix[i][j]=;
            }
        }
        if(firstLineZero)
        {
            for(int i =  ; i< matrix[].size(); i++)
            {
                matrix[][i] = ;
            }
        }
        if(firstColumnZero)
        {
            for(int i =  ; i< matrix.size(); i++)
            {
                matrix[i][] = ;
            }
        }
    }
};
73. Set Matrix Zeroes (Array)的更多相关文章
- 【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 ...
 - 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. Fo ...
 - Leetcode#73 Set Matrix Zeroes
		
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
 - [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. Follow ...
 - leetcode[73] Set Matrix Zeroes 将矩阵置零
		
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
 - LeetCode OJ 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. click ...
 - 【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. Fo ...
 - 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. 重点是空间复 ...
 - 【一天一道LeetCode】#73. Set Matrix Zeroes
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
 
随机推荐
- 作业要求20181023-4 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 02
			
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 版本控制:https://git.coding.net/liuyy08 ...
 - timer Compliant Controller  project (1)--Product introduction meeting
			
Last week ,I lead the meeting for new project. i'm very excited. The meeting is divided into the fo ...
 - Nginx 防盗链设置
			
何谓'盗链' 此内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容. 常常是一些名不见经传的小网站来盗取一些有实力的大网站的地址( ...
 - Linux修改串口irq
			
/******************************************************************************* * Linux修改串口irq * 说明 ...
 - java面试题11
			
第九次面试题 1. GC是什么?为什么要有GC? GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩 ...
 - 《DSP using MATLAB》Problem 2.5
			
2.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...
 - Python 修饰符, 装饰符
			
1, 看到@时候, 程序已经开始执行了. 所以@实际上是立即执行的 2, @后面的跟着函数名, 该函数(f1)是之前定义过的. 再后面跟着一个函数(f2), f2是f1的入口. 那么执行顺序是, ...
 - [转]Explorer.exe的命令行参数
			
本文来自:Explorer.exe的命令行参数 摘要 本文讲述explorer.exe(资源管理器)的命令行. 语法 EXPLORER.EXE [/n][/e][,/root,<object&g ...
 - 【转】httpservlet 文章
			
HttpServlet类 ellisonDon 2012-10-25 12:42 阅读:2015 评论:0 HttpServlet的功能 ellisonDon 2012-10-25 11:02 ...
 - Hive文件的存储格式
			
hive文件存储格式包括以下几类: TEXTFILE SEQUENCEFILE RCFILE 自定义格式 其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到h ...