Rotate Array

原创 2015年07月09日 14:20:23
<gs id="79dc6257-769a-42f4-81f6-b33ab08ff9a5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">class</gs> Solution <gs id="05170077-7bfc-4351-b8a4-fa2515562794" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
</gs>public:
    void <gs id="ac5838b1-a61a-4aee-82f4-fdd288dfc507" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">rotate</gs><gs id="a17fda3f-5eb1-43e9-98a9-0cfec7befc3a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>vector<<gs id="046abc55-372e-44ff-a6f9-c9e11a7e6a3a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs>>& <gs id="58754dbb-13ca-4dfc-bc17-ab61a01814a6" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs>, <gs id="d33ff531-d83e-424b-8308-8f013ff6987f" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> k) <gs id="ffb46127-ae00-4fbe-9f5a-58c825ff3aa7" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
        </gs><gs id="7ff00198-7bc4-4e89-85a0-2ce6babf5066" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> n = <gs id="5bde9243-1bbf-43bb-a9d1-d04f0520ca8c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="8865a852-2907-4ba4-9984-a6c5105a27d0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs>size<gs id="a6290452-ae3c-470c-8a4a-6ea7c12985cb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>);
        if (<gs id="0dc241d9-b716-4bf7-bba0-00eacf229b7b" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">k > n</gs>) k = k % n;
        if (n == k || k < 1) return;

        vector<<gs id="dc4d2b6a-7874-4b68-afe4-aa62ed5879c9" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs>> temp<gs id="0d00a42a-f117-4e74-9011-810c2ed25333" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs><gs id="7babb397-0ae2-48c7-9063-3b422632fba3" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="eb194a4a-5c57-4aca-94d3-c055655cf215" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs><gs id="c2e8f797-380b-44d7-a707-ee21a928609e" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">begin</gs><gs id="615e1331-58e2-41a9-8a30-8ab0a5aaefac" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>), <gs id="3844ea73-8aba-47bd-a1ac-4080eba3d6d5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="f9c18198-3aca-4c3a-a7ce-97c57bf90fc7" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">.</gs><gs id="95a965d2-e598-45b4-8ec6-163c035aef8a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">begin</gs><gs id="7e5e43a1-363c-4cb4-9124-1fc9950ebccd" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">(</gs>) + n - k);
    
        for (<gs id="ed62b337-063d-492b-ad8c-25c619114f01" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> <gs id="1eaa8f00-4bdf-4a42-a9f1-44106bab2675" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> = 0; <gs id="33bbaa53-b3a4-40d2-abb9-9c232065535c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> < k; i++) <gs id="332c2524-93e8-40ee-ae7c-c5729720f3d2" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
            </gs><gs id="c71bee51-118a-47c5-889f-9809c87aaee4" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="b4cca93c-fc0c-4be3-892c-f11057990db0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs><gs id="43b5f029-f16d-4a8b-9c68-7f6f41e68184" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>] = <gs id="c7724ef4-6b07-4262-8fcc-8860777e67ca" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="c5947857-f7cd-4c65-b51a-c4e65e0753cb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs>n - k + <gs id="e7f7d96b-74b1-4c83-8141-106e80ea91dd" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>];<gs id="8911ab8a-c5b8-4797-893d-f79de363da21" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
        }</gs>
        for (<gs id="1e99f4e0-655b-470d-9eff-54345355201c" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">int</gs> <gs id="5ee78fb7-1a06-4ef7-a400-3142abaf9028" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> = k, j = 0; <gs id="599bd548-6b40-4b30-937b-64ff41fc52eb" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs> < n; i++, j++) <gs id="48706289-7deb-4782-8350-0a3b94a37e8e" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">{
            </gs><gs id="678437c6-ad1c-41f6-b5f8-a976a5efc97a" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">nums</gs><gs id="4f9cfbf7-3f0c-47fa-bba1-e53fd12c08ca" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs><gs id="737dfb44-9e94-4afb-b3fe-436f7a710476" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">i</gs>] = temp<gs id="25009f8d-add6-4453-a5d3-d77a2c3ec4c5" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">[</gs>j];<gs id="c8c7112d-0a08-4c40-89f1-7744b909b7f0" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
        }</gs><gs id="457f0602-7a5c-4dd6-b843-ba9782d6a710" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
    }</gs><gs id="de806a80-2d5a-457e-a6e5-29fbbda86e11" ginger_software_uiphraseguid="c7367d9a-3109-4c9f-9328-f3958ab03922" class="GINGER_SOFTWARE_mark">
}</gs>;

Time O (n), space O (n);


class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int n = nums.size();
        if (k > n) k = k % n;
        if (n == k || k < 1) return;

        reverse(nums.begin(), nums.begin()+n-k);
        reverse(nums.begin()+n-k, nums.end());
        reverse(nums.begin(), nums.end());
    }
};
Time O(n), space O(1)

【LeetCode-面试算法经典-Java实现】【189-Rotate Array(旋转数组)】

【189-Rotate Array(旋转数组)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Rotate an array of n elements to the r...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月24日 07:13
  • 3003

OpenCV代码提取:rotate函数的实现

OpenCV代码提取:rotate函数的实现
  • fengbingchun
  • fengbingchun
  • 2016年07月16日 12:34
  • 6221

STL左旋转字符串Rotate的深入理解和自我实现

STL对左旋转字符串针对三种不同数据结构进行了不同的实现。 对单向链表采用的是同步位移,双向链表是三次翻转,都很简单,主要看看针对随机存取的数组做的循环位移实现。 STL这个版本的源码如下: temp...
  • chenqin158741019
  • chenqin158741019
  • 2015年03月24日 23:00
  • 1559

[Leetcode][Rotate Array]一种对连续内存空间进行的循环移位的巧妙解法(O(1)的空间、O(n)的时间)

本文最初发表:http://03071344.lofter.com/post/10871e_2c4556 本文讨论如何对一段连续的内存空间(如int型数组、char型数组)的内容进行循环移动。例...
  • l03071344
  • l03071344
  • 2013年03月22日 16:57
  • 1224

“Rotate sorted array”系列问题

给定一个排序的数组,进行数组旋转之后引出了一系列的问题,这里将遇到的相关问题做一个总结,并给出解决方法,备用所需。有序数组旋转操作这个问题是所有系列问题的起始,给一个有序数组,使用这个操作进行旋转,得...
  • u010487568
  • u010487568
  • 2016年05月05日 23:46
  • 265

[LeetCode]--189. Rotate Array

Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [...
  • bug_moving
  • bug_moving
  • 2016年10月07日 22:34
  • 513

【LeetCode笔记】Rotate Array

写了两种方法 1. 用STL函数 void rotate(int num[], int n, int k){ k %= n; if(k == 0) return...
  • Uncle_Cat
  • Uncle_Cat
  • 2015年03月10日 09:38
  • 323

Rotate Array -- leetcode

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr...
  • elton_xiao
  • elton_xiao
  • 2015年06月30日 16:17
  • 308

Rotate Array 部分旋转数组

先介绍简单的解法1: 进行观察后,可以想到一种解决方案。 1. 利用额外k空间 (这里 k = 3) , 存储[5, 6, 7 ] 2. 将前面的[1, 2, 3, 4] 后移k 位。此时arr...
  • qiexingqieying
  • qiexingqieying
  • 2016年06月12日 14:41
  • 124

rotate array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array...
  • moli152_
  • moli152_
  • 2015年03月07日 19:42
  • 162
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Rotate Array
举报原因:
原因补充:

(最多只允许输入30个字)