题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2].....

本质是将数组分成两部分a1,a2,...ak以及ak+1....an两部分,然后将两部分进行交换。

我的解法是将数组分成两部分a1,a2,.....an-k-1以及an-k,.....an,然后将两部分分别反转得到数组ank-1,.....,a2,a1,an......an-k

然后将这个数组反转得到an-k,.....an,a1,a2,....ank-1。

这个算法复杂度为O(n),空间复杂度O(1).

 class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(nums.size() == ) return;
k %= nums.size();
if( == k) return;
reverse(nums.begin(), nums.end() - k);
reverse(nums.end() - k, nums.end());
reverse(nums.begin(), nums.end());
}
};

Leetcode 189 Rotate Array stl的更多相关文章

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

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

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

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

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

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

  7. 189. Rotate Array - LeetCode

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

  8. 189. Rotate Array【easy】

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

  9. 【LeetCode】Rotate Array

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

随机推荐

  1. .NET实现高效过滤敏感查找树算法(分词算法):

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 关于Android中混淆的问题

    1.签名打包后库依赖报错,提示找不到依赖库的方法. 原因:混淆,依赖库的方法被混淆了. 解决方法:过滤混淆,即不要混淆这依赖库的文件. -keep class de.greenrobot.event. ...

  3. 屏蔽input导致的回车提交事件

    onkeypress="if(event.keyCode == 13) return false;"

  4. LoadRunner中文乱码问题解决方案

    一下内容纯属网上方法集合: 我用loadrunner录制,脚本里的乱码一直没有解决.看到网上很多贴子.采用的方法:1.第一步:去lr 的vugen的Tools -> Recoding Optio ...

  5. 磁盘配额quota应用

    1.文件系统支持 quota是针对整个文件系统来进行规划,所以我们得先查一下/home是否是个独立的文件系统. [root@Monitor home]# df -h /home Filesystem ...

  6. rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>

    大概思路如下: 使用Linux自带的rsyslog服务来做底层,然后再使用mysql与rsyslog的模板来存储文件,并且以web来进行显示出来.<模板的存储以日期的树形结构来存储,并且以服务器 ...

  7. 【.net】关于RegexOptions中的各个枚举值的含义

      Member name Description   Compiled Specifies that the regular expression is compiled to an assembl ...

  8. sql行列转换

    首先我们建立一张表,名为scoreInfo,各个字段的设计如下图,分别是name,course,score,表示姓名,成绩与分数,如图所示.

  9. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  10. Python缩进

    今天练习代码的时候发现一个问题,练习类,我在notepad++上写的代码运行后,复制到pycharm上运行然后报错,看代码 #---coding:utf-8--- #定义一个Person类然后实例化 ...