一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

(二)解题

题目大意:给定一个M*N的数组,如果(i,j)为0,则将第i行第j列全部元素置为0。

这道题目意思很简单,如果考虑到用辅助空间来计算就超级简单了,本题不允许用辅助空间!

那么,我们换个思路,利用矩阵自身来存储需要修改的信息。

这里我们考虑将需要修改的行列信息存储在第0行和第0列。具体见代码:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int row = matrix.size();
        if(row==0) return;
        int col = matrix[0].size();
        int R0flag = false;//记录第0行是否存在0
        int C0flag = false;//记录第0列是否存在0
        for(int i = 0 ; i< row ; i++)
        {
            if(matrix[i][0]==0) {C0flag=true;break;}//扫描第0列
        }
        for(int i = 0 ; i< col ; i++)
        {
            if(matrix[0][i]==0) {R0flag=true;break;}//扫描第0行
        }
        //扫描整个数组,用matrix[i][0]保存需要修改的列, matrix[0][j]保存需要修改的行
        for(int i = 0 ; i < row ;i++)
        {
            for(int j = 0 ; j < col ; j++)
            {
                if(matrix[i][j] == 0)
                {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        //修改1~col-1列
        for(int i = 1 ; i< row ; i++)
        {
            if(matrix[i][0]==0){
                for(int j = 0 ; j< col ; j++)
                {
                    matrix[i][j]=0;
                }
            }
        }
        //修改1~row-1行
        for(int i = 1 ; i< col ; i++)
        {
            if(matrix[0][i]==0){
                for(int j = 0 ; j< row ; j++)
                {
                    matrix[j][i]=0;
                }
            }
        }
        //修改第0行和第0列
        if(R0flag){
            for(int i = 0 ; i < col ; i++) matrix[0][i] =0;
        }
        if(C0flag){
            for(int i = 0 ; i < row ; i++) matrix[i][0] =0;
        }
    }
};

【一天一道LeetCode】#73. Set Matrix Zeroes的更多相关文章

  1. [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. Exampl ...

  2. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

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

  4. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  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】-- 73. Set Matrix Zeroes

    问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...

  7. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

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

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

随机推荐

  1. Java数据库开发(一)之——JDBC连接数据库

    一.MySQL数据库 1.创建数据库 CREATE DATABASE jdbc CHARACTER SET 'utf8'; 2.建表 CREATE TABLE user ( id int(10) NO ...

  2. Oracle 11g 中SQL性能优化新特性之SQL性能分析器(SQLPA)

    Oracle11g中,真实应用测试选项(the Real Application Testing Option)提供了一个有用的特点,叫SQL性能分析器(SQL Performance Analyze ...

  3. Scheme r5rs letrec的用法

    说明,这是r5rs的用法. (letrec ((<variable> <init>) ...) <body>) 假设((<variable> <i ...

  4. 安卓高级7 vitamio 视频框架 从raw文件下获取文件uri

    vitamio免费的拥有多种解码器 而且容易操作 我们先来看看原生视频播放器的怎么使用 原生的: package qianfeng.com.videoviewdemo; import android. ...

  5. activiti监听器使用

    分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519) activiti使用的时候,通常需要跟业务紧密的结合在一起,有些业 ...

  6. 6.5、Android Studio的Android Device Monitor

    Android Device Monitor是一个独立的工具,可以对Android应用进行调试和分析.Android Device Monitor无需安装整合在一个IDE中,比如像Android St ...

  7. 【java虚拟机系列】JVM类加载器与ClassNotFoundException和NoClassDefFoundError

    在我们日常的项目开发中,会经常碰到ClassNotFoundException和NoClassDefFoundError这两种异常,对于经验足够的工程师而言,可能很轻松的就可以解决,但是却不一定明白为 ...

  8. 【ShaderToy】跳动的心❤️

    写在前面 注:如果你还不了解ShaderToy,请看开篇. 作为ShaderToy系列的第一篇,我们先来点简单的.下面是效果: (CSDN目前不能传gif文件了,暂时空缺,可以看下面的原shader效 ...

  9. Fragment的事务操作&Actvity的状态丢失

    Fragment Transactions & Activity State Loss 本文翻译自Fragment Transactions & Activity State Loss ...

  10. 剑指Offer——网易笔试之不要二——欧式距离的典型应用

    剑指Offer--网易笔试之不要二--欧式距离的典型应用 前言 欧几里得度量(euclidean metric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的 ...