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. sql 语句 嵌套子查询 执行顺序分析

    --创建测试数据create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))inser ...

  2. VSFTP基线安全

    在企业级的应用中,越来越多的企业应用开源的vsftpd软件来搭建自己的文件共享服务,优点是速度快且节省开支.然而,企业用户行为难以预料,配置稍有不当则会使该服务成为一个安全风险点,导致带宽恶意占用.用 ...

  3. 用ActionSupport实现验证

    第一种: 只要Action类继承了ActionSupport,就可以用验证方案了 是个原始的方案,需要自己写代码,但是很灵活,登陆案例 不足:业务处理和验证的代码混在一起,不方便验证部分的复用和维护 ...

  4. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  5. mysql 聚集函数需要注意的问题

    1.当没有记录的时候,使用聚集函数,会导致出现一条记录,记录的取值都是NULL,如下:mysql> select name from student where name='David';Emp ...

  6. python多线程下载

    # -*- coding=utf-8 -*- import sys import os import os.path import time import urllib.request, urllib ...

  7. CentOS 7 下的软件安装建议

    https://seisman.info/how-to-install-softwares-under-centos-7.html https://seisman.info/linux-environ ...

  8. 算法_队列的Java通用数组实现

    在实现Queue的API的时候,可以使用两个实例变量做索引,一个变量head指向队列的开头,另一个变量tail指向队列的结尾.在删除一个元素的时候,使用head访问,并将head+1,插入一个元素的时 ...

  9. 关于json 的那些知识点

    深入理解JSON对象 前面的话 json(javascript object notation)全称是javascript对象表示法,它是一种数据交换的文本格式,而不是一种编程语言,用于读取结构化数据 ...

  10. 阿里云+wordpress搭建个人博客网站

    [正文] 在阿里云上搭建使用个人博客主要分为以下几个步骤: 1.购买阿里云ECS主机 2.购买域名 3.申请备案 4.环境配置 5.安装wordpress 6.域名解析 声明一下,本人对服务器端的知识 ...