Leetcode 189 Rotate Array stl
题意:将数组旋转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的更多相关文章
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
随机推荐
- iOS 设置 文字和 图片的位置
1.我最开始实现这个采用的方法:重新自定义一个view,然后有两个属性label和imageView,然后设置位置布局,再添加单击手势,用代理回传点击方法. 2.第二种方法:自定义一个Button继承 ...
- Selenium2+python自动化16-alert\confirm\prompt
前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...
- Selenium2+python自动化21-TXT数据参数化
前言 在17篇我们讲了excel数据的参数化,有人问了txt数据的参数化该怎么办呢,下面小编为你带你txt数据参数化的讲解 一.以百度搜索为例,自动搜索五次不同的关键字.输入的数据不同从而引 ...
- 新一代IDE Light Table开源:让编程工作更简单
近日,Light Table项目创始人Chris Granger在其博客上宣布Light Table开源,将代码全部托管在GitHub上,遵循GNU开源许可.与此同时,还发布了0.6版本,该版本添加了 ...
- ssh: connect to host gihub.com port 22: Connection timed out
方案1(本人使用此方案,问题得已解决): 可能是ssh-server未安装或者未启动.我的ubuntu 12.04 默认只安装了openssh-client,并没有安装server. 运行 ps -e ...
- ORA-24550错误
[oracle@app-148-39 oracledata]$ sqluldr2_linux64_10204.bin USER=xxx/xxx@xxx:1521 charset=AL32UTF8 QU ...
- 气象API(2)
中华万年历: http://wthrcdn.etouch.cn/weather_mini?city=北京通过城市名字获得天气数据,json数据http://wthrcdn.etouch.cn/weat ...
- SQL中Group By 的使用
1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...
- 快来玩“Gift大转盘”百分百赚好礼
现在开始到今年的最后一天,你天天都可以来转100%中奖的“ Gift大转盘 ”.代金券.产品折扣.精美纪念礼,没有多余规则.全部网友都可参加,转到就是你赚到,赶快转起来吧! >>活动主页& ...
- Cocos2d-x Application Wizard for Visual Studio User Guide
0. Overview Cocos2d-x-win32's project can be generated by Wizard. Wizard supports Visual Studio 2008 ...