{"id":240,"date":"2018-11-15T20:49:33","date_gmt":"2018-11-15T12:49:33","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=240"},"modified":"2018-11-29T14:50:10","modified_gmt":"2018-11-29T06:50:10","slug":"566-reshape-the-matrix","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=240","title":{"rendered":"566. Reshape the Matrix"},"content":{"rendered":"<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>\n<p>You&#8217;re given a matrix represented by a two-dimensional array, and two\u00a0<b>positive<\/b>\u00a0integers\u00a0<b>r<\/b>\u00a0and\u00a0<b>c<\/b>\u00a0representing the\u00a0<b>row<\/b>\u00a0number and\u00a0<b>column<\/b>\u00a0number of the wanted reshaped matrix, respectively.<\/p>\n<p>The reshaped matrix need to be filled with all the elements of the original matrix in the same\u00a0<b>row-traversing<\/b>\u00a0order as they were.<\/p>\n<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>\n<p><b>Example 1:<\/b><\/p>\n<pre><b>Input:<\/b> \r\nnums = \r\n[[1,2],\r\n [3,4]]\r\nr = 1, c = 4\r\n<b>Output:<\/b> \r\n[[1,2,3,4]]\r\n<b>Explanation:<\/b>\r\nThe <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.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre><b>Input:<\/b> \r\nnums = \r\n[[1,2],\r\n [3,4]]\r\nr = 2, c = 4\r\n<b>Output:<\/b> \r\n[[1,2],\r\n [3,4]]\r\n<b>Explanation:<\/b>\r\nThere is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The height and width of the given matrix is in range [1, 100].<\/li>\n<li>The given r and c are all positive.<\/li>\n<\/ol>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def matrixReshape(self, nums, r, c):\r\n        &quot;&quot;&quot;\r\n        :type nums: List[List[int]]\r\n        :type r: int\r\n        :type c: int\r\n        :rtype: List[List[int]]\r\n        &quot;&quot;&quot;\r\n        l = [[]]\r\n        if not nums:\r\n            return nums\r\n        if len(nums) * len(nums[0]) != r * c:\r\n            return nums\r\n        else:\r\n            l = [[] for i in range(r)]\r\n            count = 0\r\n            for nums_r in nums:\r\n                for nums_c in nums_r:\r\n                    a_r = int(count \/ c)\r\n                    l[a_r].append(nums_c)\r\n                    count += 1\r\n            return l\r\n                    \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In MATLAB, there is a very useful function called &#038;#821<a href=\"https:\/\/www.ccagml.com\/?p=240\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">566. Reshape the Matrix<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/240"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}