题目描述

给定一个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. matlab中ceil朝正无穷大四舍五入

    来源:https://ww2.mathworks.cn/help/matlab/ref/ceil.html?searchHighlight=ceil&s_tid=doc_srchtitle 本 ...

  2. 序列化的JavaScript

    下载 序列化的JavaScript序列化的JavaScript 将JavaScript序列化为包含正则表达式.日期和函数的JSON超集. 概述 这个包中的代码最初是作为表示状态的内部模块.为了扩展它的 ...

  3. spring-boot-route(十一)数据库配置信息加密

    Spring Boot最大的特点就是自动配置了,大大的减少了传统Spring框架的繁琐配置,通过几行简单的配置就可以完成其他组件的接入.比如你想要连接mysql数据库,只需要的配置文件里面加入mysq ...

  4. Oracle 按不同时间分组统计

    1.按年 select to_char(record_date,'yyyy'), sum(col_8) as total_money from table_name where group by to ...

  5. 2017-18一《电子商务概论》专科作业--经管B1601/2、经管B1631

    第1次作业: 1.你如何来定义和理解电子商务?电子商务对社会经济带了怎样的影响,企业.消费者的反应如何?你知道哪些电子商务企业,他们都属于什么类型? 2.请详细阐述应该如何关注哪些事项才能在淘宝网成功 ...

  6. 【源码项目】C语言编程之火车票管理系统!(最强代码)

    大学计算机软件技术基础课程设计任务书 一.题目: 火车票信息管理系统: 二.目的与要求 : ● 目的培养学生综合利用C++语言进行程序设计的能力, ● 培养学生的编程能力.用计算机解决实际问题的能力, ...

  7. 落地Azure CosmosDb的一个项目分享

    我们遇到了什么? 我们有这么一个业务场景,就是某供应商会去爬取某些数据,爬到后会发到一个FTP上,然后我们定时去获取这些数据 这个数据有大有小,小的30多M数据量百万级,大的数据量能到数百M上千万数据 ...

  8. go 数组指针 指针数组

    package main import "fmt" func test() { var p *int // 定义指针 var a = 10 p = &a // 将a的地址赋 ...

  9. ansible2.9.5使用become参数实现sudo功能

    一,为什么要使用sudo? 1, 生产环境中,为了安全因素,我们不会直接使用root来登录到server, 确实有需要的情况下,我们再使用sudo切换到root权限. 所以很多ansible的演示直接 ...

  10. C# 面试前的准备_基础知识点的回顾_01

    本系列本章来至于http://www.cnblogs.com/LionelMessi/p/4311931.html 1.try{} 里面有个Return语句,那么紧跟try后面的Finally{}会不 ...