<?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>reshape Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/reshape/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/reshape/</link>
	<description></description>
	<lastBuildDate>Thu, 16 Aug 2018 15:55:23 +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>reshape Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/reshape/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 566. Reshape the Matrix</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-566-reshape-the-matrix/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-566-reshape-the-matrix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 16 Aug 2018 15:54:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[O(mn)]]></category>
		<category><![CDATA[reshape]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3547</guid>

					<description><![CDATA[<p>Problem In MATLAB, there is a very useful function called &#8216;reshape&#8217;, which can reshape a matrix into a new one with different size but keep&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-566-reshape-the-matrix/">花花酱 LeetCode 566. Reshape the Matrix</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><iframe width="500" height="375" src="https://www.youtube.com/embed/tI8M9GO4Kvo?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1>Problem</h1>
<p>In MATLAB, there is a very useful function called &#8216;reshape&#8217;, which can reshape a matrix into a new one with different size but keep its original data.</p>
<p>You&#8217;re given a matrix represented by a two-dimensional array, and two <b>positive</b> integers <b>r</b> and <b>c</b> representing the <b>row</b> number and <b>column</b> number of the wanted reshaped matrix, respectively.</p>
<p>The reshaped matrix need to be filled with all the elements of the original matrix in the same <b>row-traversing</b> order as they were.</p>
<p>If the &#8216;reshape&#8217; operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
<b>Output:</b> 
[[1,2,3,4]]
<b>Explanation:</b>
The <b>row-traversing</b> of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b> 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
<b>Output:</b> 
[[1,2],
 [3,4]]
<b>Explanation:</b>
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
</pre>
<p><b>Note:</b></p>
<ol>
<li>The height and width of the given matrix is in range [1, 100].</li>
<li>The given r and c are all positive.</li>
</ol>
<h1>Solution1: Brute Force</h1>
<p>Time complexity: O(mn)</p>
<p>Space complexity: O(mn)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms
class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; matrixReshape(vector&lt;vector&lt;int&gt;&gt;&amp; nums, int r, int c) {
        if (nums.size() == 0) return nums;
        int m = nums.size();
        int n = nums[0].size();
        if (m * n != r * c) return nums;
        
        // new matrix r*c
        vector&lt;vector&lt;int&gt;&gt; ans(r, vector&lt;int&gt;(c));
        for(int i = 0; i &lt; m * n;++i) {
            int src_y = i / n;
            int src_x = i % n;
            int dst_y = i / c;
            int dst_x = i % c;
            ans[dst_y][dst_x] = nums[src_y][src_x];
        }
        
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-566-reshape-the-matrix/">花花酱 LeetCode 566. Reshape the Matrix</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-566-reshape-the-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
