{"id":3010,"date":"2018-07-08T00:58:53","date_gmt":"2018-07-08T07:58:53","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3010"},"modified":"2018-09-03T22:36:09","modified_gmt":"2018-09-04T05:36:09","slug":"leetcode-867-transpose-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-867-transpose-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 867. Transpose Matrix"},"content":{"rendered":"<p>Given a\u00a0matrix\u00a0<code>A<\/code>, return the transpose of\u00a0<code>A<\/code>.<\/p>\n<p>The transpose of a matrix is the matrix flipped over it&#8217;s main diagonal, switching the row and column indices of the matrix.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[[1,2,3],[4,5,6],[7,8,9]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[[1,4,7],[2,5,8],[3,6,9]]<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[[1,2,3],[4,5,6]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[[1,4],[2,5],[3,6]]<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length\u00a0&lt;= 1000<\/code><\/li>\n<li><code>1 &lt;= A[0].length\u00a0&lt;= 1000<\/code><\/li>\n<\/ol>\n<h1>Solution: Brute Force<\/h1>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; transpose(vector&lt;vector&lt;int&gt;&gt;&amp; A) {\r\n    int m = A.size();\r\n    int n = A[0].size();\r\n    vector&lt;vector&lt;int&gt;&gt; ans(n, vector&lt;int&gt;(m));\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j)\r\n        ans[j][i] = A[i][j];\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a\u00a0matrix\u00a0A, return the transpose of\u00a0A. The transpose of a matrix is the matrix flipped over it&#8217;s main diagonal, switching the row and column indices&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,216,318],"class_list":["post-3010","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-matrix","tag-transpose","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3010"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3010\/revisions"}],"predecessor-version":[{"id":3846,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3010\/revisions\/3846"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}