[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 not the best solution.
Could you devise a constant space solution?

问题:给定一个矩阵,当矩阵一个元素为 0 ,将该元素的当前行,当前列都设为 0 。

我的方案满足补充内容的第二点,使用 O(m + n) 额外空间。

  • 将矩阵中所有 0 元素的所在行,列记录下来。
  • 将记录下来的行号,整行设为0,将记录下来的列号,整列设为0.
 1 void setZeroes(vector<vector<int>>& matrix) {
 2
 3     unordered_set<int> seti;
 4     unordered_set<int> setk;
 5
 6     for (int i = 0 ; i < matrix.size(); i++) {
 7         for (int k = 0 ; k < matrix[0].size(); k++) {
 8             if (matrix[i][k] == 0) {
 9                 seti.insert(i);
10                 setk.insert(k);
11             }
12         }
13     }
14
15     unordered_set<int>::iterator s_iter;
16     for (s_iter = seti.begin(); s_iter != seti.end(); s_iter++) {
17         for (int k = 0 ; k < matrix[0].size(); k++) {
18             matrix[*s_iter][k] = 0;
19         }
20     }
21
22     for (s_iter = setk.begin(); s_iter != setk.end(); s_iter++) {
23         for (int i = 0; i < matrix.size(); i++) {
24             matrix[i][*s_iter] = 0;
25         }
26     }
27 }
时间: 2024-10-22 09:26:50

[LeetCode] 73. Set Matrix Zeroes 解题思路的相关文章

【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) spac

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

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 使用第一行和第一列记录某

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

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

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] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

Java for LeetCode 073 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. 解题思路: 用两个boolean数组row col表示行列是否有零即可,JAVA实现如下: public void setZeroes(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; boolean[] row