题意:将数组旋转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. 爬虫--scrapy--windows上安装

    关于scrapy的安装网上有一大把教程,但是比较麻烦,各种包----------,这里给大家介绍一款神器: 下载地址:http://continuum.io/downloads 根据自己电脑的系统选择 ...

  2. python学习笔记-Day6(3)

    代码书写原则: 1)不能重复写代码 2)写的代码要经常变更 编程模式概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数 ...

  3. jQuery easyui treegrid无法传参到后台bugger一记

    $("#lTreegrid").treegrid("options").queryParams={id:123456,name:"Hai he&quo ...

  4. CentOS 7下源码安装MySQL 5.7

    网上说linux安装mysql服务分两种安装方法: ①源码安装,优点是安装包比较小,只有几十M左右,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: ②使用官方编译好的二进制文件安装,优点 ...

  5. Kendo UI for ASP.NET MVC 的一些使用经验(转)

    转自 http://blog.csdn.net/dj2008/article/details/45313805 最近的项目里用到了Kendo UI.这货很好很强大,但可惜官方文档组织的并不是很好,有很 ...

  6. SDN 收集一下最近的资料

      SDN导论 SDN原理(Openflow)视频 SDN lab SDN Openflow(北航入门简介) 书籍 <深度解析SDN-利益.战略.技术.实践> -张卫峰

  7. UIView.FRAMEWORK

    uiview .framework 有太多 属性到现在基本上没怎么接触,今天开始用到了就纪录一下,有空在去了解了: [self.view bringToFront:btn] 把btn 放到self.v ...

  8. MyEclipse取消验证Js的两种途径.

    前言:有时我们通过js写一个web工程的相关页面时感觉很卡,修改内存也不行下面有俩种解决方法: 1.  选中当前工程—properties—MyEclipse—validation—Excluded ...

  9. 从一个Fragment跳转到另一个Fragment

    我们知道Activity之间的跳转可以使用 startActivity(intent).但Fragment之间的跳转却不能使用该方法,那该怎么办呢? 直接上代码: 核心代码 @Override//核心 ...

  10. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...