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.

Hint:
Could you do it in-place with O(1) extra space?

思路:通过三次翻转来实现移位

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int size = nums.size();
k %= size;
if(k == size || k == ) return; reverse(nums,,size--k);
reverse(nums,size-k,size-);
reverse(nums, , size-);
return;
} void reverse(vector<int>& nums, int start, int end){
int tmp;
while(start < end){
tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
};

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

  1. 189. Rotate Array【easy】

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

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

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

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

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

  4. 189. Rotate Array - LeetCode

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

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

  6. Java for 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 ...

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

  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. Java [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 ...

  10. C#解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  ...

随机推荐

  1. RAD Tokyo 10.2.2

    TDialogService类 如果您是使用比较新的RAD Studio版本. 那么您应该开始改用FMX.DialogService程序单元中TDialogService类别的类别方法来取代以前的Me ...

  2. 机器学习进阶-svm支持向量机

    支持向量机需要解决的问题:找出一条最好的决策边界将两种类型的点进行分开 这个时候我们需要考虑一个问题,在找到一条直线将两种点分开时,是否具有其他的约束条件,这里我们在满足找到一条决策边界时,同时使得距 ...

  3. 爬虫--urllib模块

    一.urllib库 概念:urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urll ...

  4. python中pop()函数的用法

    pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法:list.pop(obj=list[-1]) //默认为 index=-1,删除最后一个列表值. obj -- ...

  5. category,extension区别

    extension是在编译的时候就决定了的,跟普通的头文件是一样的,所以可以添加属性.实例变量.方法.并且添加extension的前提是你有这个类的源代码. category是在程序运行的时候才决定的 ...

  6. 从底层获取接口url携带的数据

    从底层获取接口url携带的数据 //实例化HttpServletRequest HttpServletRequest request = ServletHolderFilter.getContext( ...

  7. Linux tcpdump命令使用方法

    tcpdump是Linux上常用的抓包命令,用于截取网络分组并输出分组内容,常用于网络问题分析和排查. tcpdump语法 tcpdump [-i 接口] [-nn] [-w 文件名] [-c 次数] ...

  8. Delphi动态调用C++写的DLL

    c++ DLL 文件,建议用最简单的c++编辑工具.不会加入很多无关的DLL文件.本人用codeblocks+mingw.不像 VS2010,DLL编译成功,调用的时候会提示缺其他DLL. 系统生成的 ...

  9. Ajax 学习 第三篇

    1.什么是json 第一种方法 第二种方法 比较evar and jsondata 任何时候使用EVAR要特别小心,他不会管输入对象的类型 JSONLint可以在线校验代码的正确性 改写代码

  10. c# vs2013部署项目

    http://demo.netfoucs.com/shinepan/article/details/24865931