Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

(1)

/*

 * this solution is so-called three times rotate method

 * because (X^TY^T)^T = YX, so we can perform rotate operation three times to get the result

 * obviously, the algorithm consumes O(1) space and O(n) time

 */

void rotate(int nums[], int n, int k) {

    if (k<=) return;

    k %= n; 

    reverseArray(nums, n-k, n-);

    reverseArray(nums, , n-k-);

    reverseArray(nums, , n-);    

}

(2)

/*

 * How to change [0,1,2,3,4,5,6] to [4,5,6,0,1,2,3] by k = 3?

 *

 * We can change by following rules: 

 *

 *     [0]->[3], [3]->[6], [6]->[2],  [2]->[5], [5]->[1], [1]->[4]

 *    

 *

 */

void rotate(int nums[], int n, int k) {

    if (k<=) return;

    k %= n;

    int currIdx=, newIdx=k;

    int tmp1 = nums[currIdx], tmp2; 

    int origin = ;

    for(int i=; i<n; i++){

        tmp2 = nums[newIdx];

        nums[newIdx] = tmp1;

        tmp1 = tmp2; 

        currIdx = newIdx;

        //if we meet a circle, move the next one

        if (origin == currIdx) {

            origin = ++currIdx;

            tmp1 = nums[currIdx];

        }

        newIdx = (currIdx + k) % n;

    } 

}

189. Rotate Array -- 将数组前一半移到后一半的更多相关文章

  1. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  2. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  3. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  4. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  5. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  6. LeetCode Rotate Array 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

  7. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  8. 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  ...

  9. 189. Rotate Array 从右边开始翻转数组

    [抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

随机推荐

  1. CSS3教程链接

    下面列出本站关于CSS3的相关链接,以方便大家阅读: 第一节:<CSS3 Gradient> 第二节:<CSS3 RGBA> 第三节:<CSS3 Border-radiu ...

  2. 【转】基于APD的光电探测器电路研究与设计

    光电探测器电路用于对光电转换器件输出的微弱电压或电流信号进行放大.处理和整形输出.对于不同探测用途而采用的光电转换器件不同,与之配合使用的光电探测器电路性能也因此而不同.如果用来进行光电转换,则重点考 ...

  3. 线程入门之join方法

    package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...

  4. Codeforces Round #158 (Div. 2) C. Balls and Boxes 模拟

    C. Balls and Boxes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. react入门笔记

    this.props.children是任何内嵌的元素 利用ref属性给子组件命名,this.refs引用组件,getDOMNode()获取本地的DOM元素,如: this.refs.author.g ...

  6. svn使用(服务器端和客户端)

    http://www.cnblogs.com/tugenhua0707/p/3969558.html 网址如上. 很详细.

  7. Andoid java文件中的Log检查工具

    AndroidLogChecker 由于发布软件版本的时候我们需要把Log注释掉,此工具可以检查java类中的Log所在行以及是否已经注释. Github: https://github.com/cu ...

  8. iOS - Swift NSNumber 数字

    前言 public class NSNumber : NSValue public class NSDecimalNumber : NSNumber NSNumber 可以被赋值为各种数值类型.我们可 ...

  9. Spring 读书笔记-----使用Spring容器(一)

    pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...

  10. 基于Linux的oracle数据库管理 part6 (backup 相关的脚本)

    这里只是简单的介绍几种 备份方法 备份: 逻辑备份, 冷备份, 热备份 逻辑备份 也称作 导入(import), 导出(export), 作用是在不同的oracle数据库之间转移数据 物理备份, 就是 ...