【LeetCode】Set Matrix Zeroes 解题报告

今天看到CSDN博客的勋章换了图表,同时也增加显示了博客等级,看起来都听清新的,感觉不错!

【题目】

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(m + n) space, but still not the best solution.

Could you devise a constant space solution?

【思路】

我直接看了下面的要求,最先想到的就是O(m+n)的空间,就是加一行一列来标记哪行哪列有0;反而是O(mn)的空间不知该怎么利用了。

接下来想O(1)空间,稍微一想,竟然想到了答案,就是相比用O(m+n)的空间,不额外加一行一列,就用第一行和第一列来存储哪行哪列有0。当然,这样做比较麻烦的是第一行第一列的原始信息,需要先保存下来。

前两次写的时候,由于第一行第一列没有处理好,导致WA。

AC后看网上答案,看到其他人也是这么一个思路,挺高兴的。

【Java代码,O(1)空间】

public class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int i, j;

        //先标记第一行和第一列是否有0
        boolean firstRow = false, firstCol = false;
        for (j = 0; j < n; j++) {
            if (0 == matrix[0][j]) {
                firstRow = true;
                break;
            }
        }
        for (i = 0; i < m; i++) {
            if (0 == matrix[i][0]) {
                firstCol = true;
                break;
            }
        }

        //从第二行第二列还是遍历,如果遇到0,则把它所在行和列的第一个值设为0
        for (i = 1; i < m; i++) {
            for (j = 1; j < n; j++) {
                if (0 == matrix[i][j]) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        //把第一列的0所在行都设为0,把第一行的0所在列都设为0
        for (i = 1; i < m; i++) {
            if (0 == matrix[i][0]) {
                for (j = 1; j < n; j++) {
                    matrix[i][j] = 0;
                }
            }
        }
        for (j = 1; j < n; j++) {
            if (0 == matrix[0][j]) {
                for (i = 1; i < m; i++) {
                    matrix[i][j] = 0;
                }
            }
        }

        //根据标记决定第一行和第一列是否全设为0
        if (firstRow) {
            for (j = 0; j < n; j++) {
                matrix[0][j] = 0;
            }
        }
        if (firstCol) {
            for (i = 0; i < m; i++) {
                matrix[i][0] = 0;
            }
        }
    }
}
时间: 2024-10-11 21:21:06

【LeetCode】Set Matrix Zeroes 解题报告的相关文章

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

[leetcode] 283. Move Zeroes 解题报告

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

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】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl