{"id":3456,"date":"2018-08-06T08:22:05","date_gmt":"2018-08-06T15:22:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3456"},"modified":"2018-08-06T08:24:14","modified_gmt":"2018-08-06T15:24:14","slug":"leetcode-189-rotate-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-189-rotate-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 189. Rotate Array"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array, rotate the array to the right by\u00a0<em>k<\/em>\u00a0steps, where\u00a0<em>k<\/em>\u00a0is non-negative.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input:<\/strong> [1,2,3,4,5,6,7] and k = 3 \r\n<strong>Output:<\/strong> [5,6,7,1,2,3,4]\r\n<strong>Explanation:<\/strong> rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] \r\nrotate 3 steps to the right: [5,6,7,1,2,3,4]<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>[-1,-100,3,99] and k = 2 \r\n<strong>Output:<\/strong> [3,99,-1,-100] \r\n<strong>Explanation:<\/strong> rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.<\/li>\n<li>Could you do it in-place with O(1) extra space?<\/li>\n<\/ul>\n<h1><strong>Solution 1: Simulate rotation with three reverses.<\/strong><\/h1>\n<p>If k &gt;= n, rotating k times has the same effect as rotating k % n times.<\/p>\n<p>[1,2,3,4,5,6,7], K = 3<\/p>\n<p>[5,6,7,1,2,3,4]<\/p>\n<p>We can simulate the rotation with three reverses.<\/p>\n<ol>\n<li>reverse the whole array O(n) [<strong><span style=\"color: #ff0000;\">7,6,5<\/span><\/strong>,<strong><span style=\"color: #000080;\">4,3,2,1<\/span><\/strong>]<\/li>\n<li>reverse the left part 0 ~ k &#8211; 1 O(k) [5,6,7,<strong><span style=\"color: #000080;\">4,3,2,1]<\/span><\/strong><\/li>\n<li>reverse the right part k ~ n &#8211; 1 O(n-k)\u00a0[5,6,7,1,2,3,4]<\/li>\n<\/ol>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1) in-place<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  void rotate(vector&lt;int&gt;&amp; nums, int k) {\r\n    if (nums.empty()) return;\r\n    k %= nums.size();\r\n    if (k == 0) return;\r\n    reverse(begin(nums), end(nums));\r\n    reverse(begin(nums), begin(nums) + k);\r\n    reverse(begin(nums) + k, end(nums));\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array, rotate the array to the right by\u00a0k\u00a0steps, where\u00a0k\u00a0is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate&#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":[222,246,367,179],"class_list":["post-3456","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-reverse","tag-rotation","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3456","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=3456"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3456\/revisions"}],"predecessor-version":[{"id":3460,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3456\/revisions\/3460"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}