leetcode 189
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,2,3,4,5,6,7]
is rotated to [5,6,7,1,2,3,4]
.
将数组向右旋转k位。
代码如下:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k%n;
if(k){
vector<int> p,q;
for(int i=n-k;i<n;i++){
p.push_back(nums[i]);
}
for(int i=;i<n-k;i++){
q.push_back(nums[i]);
}
for(int i=;i<k;i++){
nums[i]=p[i];
}
for(int i=k;i<n;i++){
nums[i]=q[i-k];
}
}
}
};
leetcode 189的更多相关文章
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
- [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)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 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 旋转数组
189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] ...
- 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,.. ...
- 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 ...
随机推荐
- Qt自定义事件的实现(转)
原文:http://blog.csdn.net/michealtx/article/details/6866094 初学Qt,用了Qt自带的事件,然后想怎么才能定义自己的事件呢?又如何使用自定义事件呢 ...
- updatepanel 回发或回调参数无效
不同于网上的其它情况,这个是由于通过js修改了服务器控件Select的列表项数目,导致验证viewstate时出现的问题.最后改为通过服务器代码来给Select加选项,就不会报这个错误了. 服务器控件 ...
- 免费SSL-HTTS 申请与配置 NGINX配置
Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期.适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示.Let's Encrypt已经发 ...
- nginx 反向代理TCP mysql
stream {upstream mysql { hash $remote_addr consistent; server 10.26.112.12:3306 max_fails=3 fail_tim ...
- iso 培训笔记
protocol协议 + 类方法- 实例方法:继承()方法 <> [] 方法调用 .属性:参数mvc storyboard 资源文件info.plist 权限viewcontroller ...
- nginx下rewrite参数超过9个的解决方法
nginx 在处理多于9个参数的时候,是采用重命名的方法来实现的: /?m?([0-9,]*)h?(\d*)a?([0-9,]*)c?(\d*)s?(x?f?(?P<f>[0-9,]*)/ ...
- webstorm(注册,激活,破解,码,一起支持正版,最新可用)(2016.9.2更新)
webstorm(注册,激活,破解,码,一起支持正版,最新可用)(2016.9.2更新) 来源于:http://blog.csdn.net/xx1710/article/details/51725 ...
- VS2010中App_Code文件夹的问题
在VS2010中新建一个Web Application,然后新建一个app_Code文件夹, 在app_code文件夹下建一个ClassHelper类. 然后在index页面中使用ClassHelpe ...
- MVC4 +EasyUI 使用TreeGrid 方法
用easyui已经有2年了,换了新环境,要求用mvc开发,所以想把原来的项目直接用mvc重构. 在使用TreeGird的时候出现了问题,发现在转换为treegrid的json 很费劲,一直都是用的ea ...
- 生成均值文件mean.binaryproto
[感谢:http://www.cnblogs.com/denny402/p/5102328.html] compute_image_mean.bin生成均值文件mean.binaryproto: ca ...