题目描述

给定一个m*n的矩阵,如果有一个元素是0,就把该元素所在的行和列上的元素全置为0,要求使用原地算法。
拓展:
你的算法有使用额外的空间吗?
一种比较直接的算法是利用O(m,n)的空间,但是这不是一个好的解法
使用简单的改进可以在O(m+n)的空间解决这个问题,但是还不是最佳的解法
你能在常量级的空间复杂度内解决这个问题吗?

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Follow up: Did you use extra space?
A straight forward solution using O(m n) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        const int row=matrix.size();
        const int col=matrix[0].size();
        bool row_flag=false,col_flag=false;
        
        for (int i=0;i<row ;i++)
            if (0==matrix [i][0])
            {
                col_flag=true;
                break;
            }
            
        
        for (int i=0;i<col;i++)
            if (0==matrix[0][i])
            {
                row_flag=true;
                break;
            }
        
        for (int i=1;i<row;i++)
            for (int j=1;j<col;j++)
                if (0==matrix[i][j]){
                    
                
                    matrix[i][0]=0;
                    matrix[0][j]=0;
                }
                
            
            
        
        for (int i=1;i<row;i++){
            for (int j=1;j<col;j++){
                if (0==matrix[i][0] || matrix [0][j]==0)
                    matrix[i][j]=0;
            }
        }
        if (row_flag)
            for (int i=0;i<col;i++)
                matrix[0][i]=0;
        if (col_flag)
            for (int i=0;i<row;i++)
                matrix[i][0]=0;
        
    }
};

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

  10. [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. click ...

随机推荐

  1. 1个LED灯闪烁的Arduino控制

    控制任务和要求 让一个LED灯闪烁 接线 程序设计 1 int half_cycle=1000; // define the cycle time of LED blink 2 int LED_pin ...

  2. springcloud学习入门

    Springcloud入门学习笔记 1. 项目初始化配置 1. 1. 新建maven工程 使用idea创建maven项目 1. 2. 在parent项目pom中导入以下依赖 <parent> ...

  3. AD(Altium Designer)PCB布线中的“格式刷”,助力快速布局布线

    摘要:在AD(Altium Designer)进行电路板布线时,孔丙火(微信公众号:孔丙火)经常会碰到电路中有相同功能的模块,比如2路相同的RS485通信电路.多路相同继电器输出电路.多路相同的输入电 ...

  4. 启动你的Android应用:运行设备模拟器和调试代码(第3部分)

    下载all source for Test Proj: Test.zip - 306 KB 文章指出 本文包含了我即将出版的新书<启动Android应用程序>中的第三章. 在我完成这本书之 ...

  5. CodeForces 79D 【Password】,洛谷P3943 【星空】

    其实我做的是洛谷的P3943,但是听说fstqwq窃题...... 题目描述: 小 C 拿来了一长串星型小灯泡,假装是星星,递给小 F,想让小 F 开心一点.不过,有 着强迫症的小 F 发现,这串一共 ...

  6. # vue 如何通过前端来导出excel表格

    在做一些简单的demo时,偶尔会遇到导出excel表格.如果请后端帮忙的话 比较浪费时间,那么前端如何导出excel表格,下面就来记录一下之前使用到的案例 一.安装依赖 npm i file-save ...

  7. S3C2440 LCD驱动(FrameBuffer)实例开发<二>(转)

    开发板自带的LCD驱动是基于platform总线写的,所以如果要使其它的LCD能够在自己的开发板上跑起来,那么就先了解platform驱动的架构,下面简单记录下自己看platform驱动时体会,简单的 ...

  8. 多测师讲解接口测试 _linux中搭建环境cms_高级讲师肖sir

    cms后台的搭建, 我们主要是用来做接口测试和接口自动化测试: 我们搭建的流程和之前搭建多有米一样: 后期给我们一个war包就要知道搭建一个文档: 要知道环境项目怎么讲?   讲解下搭建cms环境的流 ...

  9. Mysql索引(一篇就够le)

    我想很多人对mysql的认知可能就是CRUD(代表创建(Create).更新(Update).读取(Retrieve)和删除(Delete)操作),也不敢说自己会用和熟悉mysql,当然我就是其中一个 ...

  10. scrapy LinkExtractors

    class scrapy.linkextractors.LinkExtractor Link Extractors 的目的很简单: 提取链接。 每个LinkExtractor有唯一的公共方法是 ext ...