题目描述

给定一个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. sqlserver 分列

    sql server 数据库中某张表(Person)的数据信息是: ID Address 1 平山花园-4单元-12幢-203 2 香山花园-3单元-22幢-304 现在有需求是,将地址信息显示形式改 ...

  2. 3-kubernetes监控与日志管理

    监控集群资源利用率 metrics-server是一个集群范围的资源使用情况的数据聚合器,作为一个应用部署在集群中 metrics-server从每个节点上kubelet API收集指标,通过kube ...

  3. IDEA设置maven修改settings.xml配置文件无法加载仓库

    作为初学者配置maven一般网上搜索.然后你就看到各种配置文件片段,首先配置镜像,然后配置仓库.完事后再IDEA里面配置下maven的路径和配置文件路径. 这些文章属实坑爹,完全没讲一个重要的配置就是 ...

  4. 多测师讲解html _段落标签002_高级讲师肖sir

    <html> <head> <meta charset="UTF-8"> <title>段落标签</title> < ...

  5. js产生任意2个区间内的随机整数

    var code = myRound(30,100); function myRound(begin,end){ var num = Math.round(Math.random()*(end-beg ...

  6. Springboot配置excludePathPatterns不生效

    Springboot添加拦截器配置excludePathPatterns不生效 code: @Configurationpublic class ServiceConfig implements We ...

  7. tamcat7.0(安装文件下载)

    安装包:http://files.cnblogs.com/files/chenghu/apache-tomcat-7.0.27.rar http://files.cnblogs.com/files/c ...

  8. CentOS7通过源码安装nginx

    需要先安装安装环境和库: yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum i ...

  9. SSM中 spring.xml 配置文件

    <!--扫描service的impl--><context:component-scan base-package="com.aaa.ssm.service.impl&qu ...

  10. Disconnected from the target VM, address: '127.0.0.1:1135', transport: 'socket'-SpringBoot启动报错

    一.问题由来 本地代码在一次打包后,再次启动项目时报了一个错误,详细的错误信息如下: 2020-10-23 15:10:26.724 [] [main] INFO o.s.c.a.Annotation ...