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.

解答

这题太水了,根本不是medium难度,一遍就AC了。

遍历matrix,拿两个数组分别记录需要变成0的行和列就OK了。

下面是AC的代码:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        vector<int> row, col;
        int m = matrix.size();
        if(m == 0){
            return ;
        }
        int n = matrix[0].size();

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(matrix[i][j] == 0){
                    row.push_back(i);
                    col.push_back(j);
                }
            }
        }
        for(vector<int>::iterator iter = row.begin(); iter != row.end(); iter++){
            for(int i = 0; i < n; i++){
                matrix[*iter][i] = 0;
            }
        }
        for(vector<int>::iterator iter = col.begin(); iter != col.end(); iter++){
            for(int i = 0; i < m; i++){
                matrix[i][*iter] = 0;
            }
        }
    }
};

113

原文地址:https://www.cnblogs.com/YuNanlong/p/8802038.html

时间: 2024-11-08 15:56:27

LeetCode OJ 73. Set Matrix Zeroes的相关文章

【一天一道LeetCode】#73. Set Matrix Zeroes

一天一道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. 这道题目意思很简单,如果考虑

【LeetCode】-- 73. Set Matrix Zeroes

问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant space 的辅助空间.自然想到位优化.一个int可以存储31个元素的信息.这里刚好用到了字符串论文里面常用的优化处理方法.类似桶分的思想.好吧,这么看来这长时间的论文没白看. 附上代码: 1 void setZeroes(vector<vector<int>>& matrix

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

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. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

leetcode第一刷_Set Matrix Zeroes

这个题乍一看很简单,实际上还挺有技巧的.我最开始的想法是找一个特殊值标记,遇到一个0,把他所对应的行列中非零的元素标记成这个特殊值,0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题是这个特殊值怎么确定,题目中没有把取值范围给出,我怀着侥幸的心理用了最大和最小的int,都被揪了出来..如果找一个不存在于数组中的值,这个复杂度太高了. 有没有其他更好的方法呢?当然有.这个思想很巧妙,最后的结果是把所有0所在的行列都化成0,换句话说,化成0这个事情只要标记出是哪一行以及哪一列就可以了,能

73. Set Matrix Zeroes(js)

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. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: [   [0,1,2,0],   [

Leetcode 细节实现 Set Matrix Zeroes

Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. 题意:给定矩阵,如果矩阵的某个位置为0,则把那一行那一列的所有元素都置为0 思路:用两个bool数组,分

[LeetCode#73]Set Matrix Zeroes

The problem: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. My analysis: This is a very typical quesition in metricing our understanding about matrix. The main idea is that : We could not base on the cha

[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 up: 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

leetcode 73 Set Matrix Zeroes ----- java

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O