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

题目:

给一数组,n个元素,将数组向右移动循环移动k个元素。

思路:

注意题目的要求是循环移动,所以k可以是任意非负整数,但数组元素个数为n,因此k=k%n。

一种通用的思路就是:

1、翻转整个数组 :A[0…n-1];如[7,6,5,4,3,2,1]

2、翻转数组前k个:A[0,k-1];如[5,6,7,4,3,2,1]

3、翻转数组后n-k个:A[k,n-1];如[5,6,7,1,2,3,4]

类似的应用:翻转句子中的全部单词,单词内容不变。

代码:

class Solution {
public:
void reverse(int nums[],int i,int j){
while(i<j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
i++;
j--;
}
} void rotate(int nums[], int n, int k) {
k=k%n;
if(k>= && n> && k<=n){
reverse(nums,,n-);
reverse(nums,,k-);
reverse(nums,k,n-);
}
}
};

(LeetCode 189)Rotate Array的更多相关文章

  1. 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 arr ...

  2. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  3. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  4. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. LeetCode OJ:Rotate Array(倒置数组)

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

  6. (LeetCode 153)Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

  8. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  9. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

随机推荐

  1. 洛谷 P4884 多少个1?

    题面在这里 好久没做题了2333,竟然还一次A了,神奇 大概就是等比数列然后把分母乘过去,然后直接BSGS就行了,就是要写快速乘恩... #include<bits/stdc++.h> # ...

  2. bzoj 3956: Count

    3956: Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N< ...

  3. 鸟哥的私房菜:Linux磁盘与文件系统原理

    1 硬盘物理组成     //原理 磁头负责读写     磁道(硬盘同半径的一圈) 磁柱(所有盘磁道叠加起来的柱)     扇区(2条半径将磁道分开的一个扇形区域,是磁盘的最小存储单位) ------ ...

  4. 【Codeforces528D】Fuzzy Search FFT

    D. Fuzzy Search time limit per test:3 seconds memory limit per test:256 megabytes input:standard inp ...

  5. android显示TextView文字的倒影效果

    今天记录一下TextView的倒影效果,显示一串文字,然后在文字的下方显示出它的倒影,先上效果图: 最重要的就是View中getDrawingCache()方法,该方法可以获取cache中的图像,然后 ...

  6. Codeforces Round #243 (Div. 1)A. Sereja and Swaps 暴力

    A. Sereja and Swaps time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. flex socket policy

    @ flex的as3代码是具备使用origin tcp socket通信能力的. @ 如果是flex builder本机调试,那么可以直连tcp的server. @ 如果flex发布在webserve ...

  8. Xilinx Platform Usb Cable

    Key Features High-performance FPGA configuration and PROM/CPLD programming Includes innovative FPGA- ...

  9. 用资源管理器右键编译 Visual Studio 解决方案文件

    每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...

  10. C#程序集系列13,如何让CLR选择不同版本的程序集

    本篇主要体验,在存在多个版本程序集的情况下,如何让CLR选择哪个版本程序集运行,以及程序集版本的切换. 分别生成非强名称程序集不同版本 □ 生成某个版本的程序集 →清理F盘as文件夹,剩下如下文件 → ...