<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>constant space Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/constant-space/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/constant-space/</link>
	<description></description>
	<lastBuildDate>Sat, 09 Feb 2019 19:56:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>constant space Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/constant-space/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 73. Set Matrix Zeroes</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-73-set-matrix-zeroes/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-73-set-matrix-zeroes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 09 Feb 2019 19:32:10 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[constant space]]></category>
		<category><![CDATA[in place]]></category>
		<category><![CDATA[O(1)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4806</guid>

					<description><![CDATA[<p>Given a&#160;m&#160;x&#160;n&#160;matrix, if an element is 0, set its entire row and column to 0. Do it&#160;in-place. Example 1: Input: [ &#160; [1,1,1], &#160; [1,0,1],&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-73-set-matrix-zeroes/">花花酱 LeetCode 73. Set Matrix Zeroes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<em>m</em>&nbsp;x&nbsp;<em>n</em>&nbsp;matrix, if an element is 0, set its entire row and column to 0. Do it&nbsp;<a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noreferrer noopener"><strong>in-place</strong></a>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input:</strong> 
[
&nbsp; [1,1,1],
&nbsp; [1,0,1],
&nbsp; [1,1,1]
]
<strong>Output:</strong> 
[
&nbsp; [1,0,1],
&nbsp; [0,0,0],
&nbsp; [1,0,1]
]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input:</strong> 
[
&nbsp; [0,1,2,0],
&nbsp; [3,4,5,2],
&nbsp; [1,3,1,5]
]
<strong>Output:</strong> 
[
&nbsp; [0,0,0,0],
&nbsp; [0,4,5,0],
&nbsp; [0,3,1,0]
]
</pre>



<p><strong>Follow up:</strong></p>



<ul><li>A straight forward solution using O(<em>m</em><em>n</em>) space is probably a bad idea.</li><li>A simple improvement uses O(<em>m</em>&nbsp;+&nbsp;<em>n</em>) space, but still not the best solution.</li><li>Could you devise a constant space solution?</li></ul>



<h2><strong>Solution 1</strong></h2>



<p>Use two arrays to track whether the i-th row / j-th column need to be zeroed.</p>



<p>Time complexity: O(mn)<br>Space complexity: O(m+n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  void setZeroes(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int m = matrix.size();
    const int n = matrix[0].size();
    vector&lt;int&gt; rows(m);
    vector&lt;int&gt; cols(n);
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        rows[i] |= (matrix[i][j] == 0);
        cols[j] |= (matrix[i][j] == 0);
      }
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (rows[i] || cols[j]) matrix[i][j] = 0;        
  }
};</pre>
</div></div>



<h2><strong>Solution 2</strong></h2>



<p>Use the first row / first col to indicate whether the i-th row / j-th column need be zeroed.</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  void setZeroes(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int m = matrix.size();
    const int n = matrix[0].size();
    
    bool col0 = false;
    bool row0 = false;
    
    for (int i = 0; i &lt; m; ++i) 
      col0 |= (matrix[i][0] == 0);
    for (int j = 0; j &lt; n; ++j) 
      row0 |= (matrix[0][j] == 0);
    
    for (int i = 1; i &lt; m; ++i)
      for (int j = 1; j &lt; n; ++j)
        if (matrix[i][j] == 0)
          matrix[0][j] = matrix[i][0] = 0;      
    
    for (int i = 1; i &lt; m; ++i)
      for (int j = 1; j &lt; n; ++j)
        if (matrix[i][0] == 0 || matrix[0][j] == 0)
          matrix[i][j] = 0;
    
    if (row0)
      for (int j = 0; j &lt; n; ++j) 
        matrix[0][j] = 0;
    
    if (col0)
      for (int i = 0; i &lt; m; ++i) 
        matrix[i][0] = 0;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-73-set-matrix-zeroes/">花花酱 LeetCode 73. Set Matrix Zeroes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-73-set-matrix-zeroes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
