Nav: << previous: 72.编辑距离 | next: 74.搜索二维矩阵 >>


Description

tab: English
 
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p>
 
<p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg" style="width: 450px; height: 169px;" />
<pre>
<strong>Input:</strong> matrix = [[1,1,1],[1,0,1],[1,1,1]]
<strong>Output:</strong> [[1,0,1],[0,0,0],[1,0,1]]
</pre>
 
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg" style="width: 450px; height: 137px;" />
<pre>
<strong>Input:</strong> matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
<strong>Output:</strong> [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[0].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>-2<sup>31</sup> &lt;= matrix[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
 
<p>&nbsp;</p>
<p><strong>Follow up:</strong></p>
 
<ul>
	<li>A straightforward solution using <code>O(mn)</code> space is probably a bad idea.</li>
	<li>A simple improvement uses <code>O(m + n)</code> space, but still not the best solution.</li>
	<li>Could you devise a constant space solution?</li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> If any cell of the matrix has a zero we can record its row and column number using additional memory.
 
But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says.
 
> [!tip]- Hint 2
> 
> Setting cell values to zero on the fly while iterating might lead to discrepancies. What if you use some other integer value as your marker?
 
There is still a better approach for this problem with O(1) space.
 
> [!tip]- Hint 3
> 
> We could have used 2 sets to keep a record of rows/columns which need to be set to zero. But for an O(1) space solution, you can use one of the rows and and one of the columns to keep track of this information.
 
> [!tip]- Hint 4
> 
> We can use the first cell of every row and column as a flag. This flag would determine whether a row or column has been set to zero.
 
 
---
 
[submissions](https://leetcode.com/problems/set-matrix-zeroes/submissions/) | [solutions](https://leetcode.com/problems/set-matrix-zeroes/solutions/)
 
 
tab: 中文
 
<p>给定一个&nbsp;<code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong>。</strong></p>
 
<ul>
</ul>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg" style="width: 450px; height: 169px;" />
<pre>
<strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]
<strong>输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]
</pre>
 
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg" style="width: 450px; height: 137px;" />
<pre>
<strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
<strong>输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[0].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>-2<sup>31</sup> &lt;= matrix[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
 
<p>&nbsp;</p>
 
<p><strong>进阶:</strong></p>
 
<ul>
	<li>一个直观的解决方案是使用 &nbsp;<code>O(<em>m</em><em>n</em>)</code>&nbsp;的额外空间,但这并不是一个好的解决方案。</li>
	<li>一个简单的改进方案是使用 <code>O(<em>m</em>&nbsp;+&nbsp;<em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li>
	<li>你能想出一个仅使用常量空间的解决方案吗?</li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> If any cell of the matrix has a zero we can record its row and column number using additional memory.
 
But if you don't want to use extra memory then you can manipulate the array instead. i.e. simulating exactly what the question says.
 
> [!tip]- 提示 2
> 
> Setting cell values to zero on the fly while iterating might lead to discrepancies. What if you use some other integer value as your marker?
 
There is still a better approach for this problem with O(1) space.
 
> [!tip]- 提示 3
> 
> We could have used 2 sets to keep a record of rows/columns which need to be set to zero. But for an O(1) space solution, you can use one of the rows and and one of the columns to keep track of this information.
 
> [!tip]- 提示 4
> 
> We can use the first cell of every row and column as a flag. This flag would determine whether a row or column has been set to zero.
 
 
---
 
[提交记录](https://leetcode.cn/problems/set-matrix-zeroes/submissions/) | [题解](https://leetcode.cn/problems/set-matrix-zeroes/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122