LeetCode -- Set Matrix Zeroes

Question:

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

Analysis:

给出一个m * n的矩阵,如果一个元素为0,则它整行整列都设为0.

Answer:

public class Solution {
    public void setZeroes(int[][] matrix) {
        ArrayList<Integer> colll = new ArrayList<Integer>();
        if(matrix == null)
            return;

        int row = matrix.length, col = matrix[0].length;
        boolean flag = false;
        for(int i=0; i<row; i++) {
            flag = false;
            for(int j=0; j<col; j++) {
                if(matrix[i][j] == 0) {
                    flag = true;
                    if(!colll.contains(j))
                        colll.add(j);
                }
            }
            if(flag == true) {
                for(int j=0; j<col; j++)
                matrix[i][j] = 0;
            }
        }

        for(int j=0; j<colll.size(); j++) {
            int temp = colll.get(j);
            for(int i=0; i<row; i++)
                matrix[i][temp] = 0;
        }
    }
}
时间: 2024-10-14 15:09:24

LeetCode -- Set Matrix Zeroes的相关文章

LeetCode: Set Matrix Zeroes [073]

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. [题意] 给定一个mXn的矩阵,如果其中的元素为0,则对应的行和列都用0填充. 不能申请额外的空间. [思路] 第一行和第一列空出来标记需要置0的列和行 第一遍扫描: 扫描第一行,判断第一行是否需要清零 扫描第一列,判断第一列是否需要清零 扫描[1,1]~[m-1, n-1]的区块,如果某个位置上的值

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. 原题链接:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题目:给定一个m * n 的矩阵,如果有一个元素是0,将其所在行和列设为0. 思路:先记录下是0 的元素的位置,再去置0. public void setZeroes(int[][] matrix)

[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 to show follow up. 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

LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题目没有什么难度,但是可以在空间复杂度上做一些处理: 开始写的算法比较简单,将行和列中为0的部分记录下来,然后再经过一个赋值操作: class Solution { public: void setZeroes(vector<vector<int> > &matrix) { if(matr

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

LeetCode 第 73 题 (Set Matrix Zeroes)

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 impro

【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. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

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