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. javascript中的立即执行函数(function(){…})()

    javascript中的立即执行函数(function(){…})() 深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包 ...

  2. filter map reduce函数的使用

    #filter("处理逻辑","可迭代对象") 把可迭代对象依次处理逻辑处理,如果值为真就返回值,值为假就不返回; li = ['testA','yerA',' ...

  3. python内置函数使用

    print(abs(1)) #绝对值,正数就是自己 ",''])) #计算可迭代对象中是否为真,其中一个为假,就显示为假 print(all('')) # If the iterable i ...

  4. 玩转音频、视频的利器:FFmpeg

    导语 当下直播平台发展十分迅猛,依靠游戏内直播平台的发展带动游戏活跃提升收入,那么对于我们开发来说如何玩转视频呢?下面就来介绍一个音频.视频处理利器——FFmpeg. FFmpeg 简介 FFmpeg ...

  5. python 如何获取当前文件/文件夹

    python 如何获取当前文件/文件夹? 1.获取当前文件的实际路劲: os.path.realpath(__file__)          ==> D:\python_test\test_p ...

  6. delphi java 日期 转换 获取Unix时间戳

    获取Unix时间戳 http://www.cnblogs.com/findumars/p/4716753.html 最简单准确一句话 Result:=IntToStr(  DateTimeToUnix ...

  7. FDQuery多表更新生成sql语句的问题

    query.sql='select  a,b,c,d,e from a,b,c where ....'; 来源3个表, 设计时添加字段列表,每个字段有Origin属性 分别是a.a,b.b,c.c格式 ...

  8. 深度学习原理与框架-卷积网络细节-图像分类与图像位置回归任务 1.模型加载 2.串接新的全连接层 3.使用SGD梯度对参数更新 4.模型结果测试 5.各个模型效果对比

    对于图像的目标检测任务:通常分为目标的类别检测和目标的位置检测 目标的类别检测使用的指标:准确率, 预测的结果是类别值,即cat 目标的位置检测使用的指标:欧式距离,预测的结果是(x, y, w, h ...

  9. Java中==号与equals()方法的区别

    String str1 = new String("abc"); String str2 = new String("abc"); System.out.pri ...

  10. 固定顶部指定div不滑动

    .fixed_div { position:fixed; z-index:100; top: 45px; width:100%; height:45px; } 指定div设置属性position:fi ...