leetcode 189. Rotate Array
2016-04-10 19:26
316 查看
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array
rotated to
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.size()<2)
return;
if ( k <= 0)
return;
k = k%nums.size();
vector<int>aa = vector<int>(nums.begin(), nums.end()-k);
for (int i = nums.size() - 1;i>=nums.size() -k; i--)
aa.insert(aa.begin(),nums[i]);
nums = aa;
}
};
accept
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.
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.size()<2)
return;
if ( k <= 0)
return;
k = k%nums.size();
vector<int>aa = vector<int>(nums.begin(), nums.end()-k);
for (int i = nums.size() - 1;i>=nums.size() -k; i--)
aa.insert(aa.begin(),nums[i]);
nums = aa;
}
};
accept
相关文章推荐
- 在计算无处不在的时代,双因子认证方式(2FA)可能不再安全!
- Jmeter 结合 Servers Performance Monitoring 插件进行性能监控
- Out.flush用法
- Android Studio调试功能使用总结
- Android 更新UI的两种方法——handler和runOnUiThread()
- archlinux xfce4 l2tp服务启动失败
- CQOI2016 总结
- 【剑指offer系列】 把数组排成最小的数___33
- bug记录1
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
- 团队博客 一 需求分析
- 牺牲空间换时间的非比较排序之计数排序和基数排序
- Android ServiceConnection详解
- POJ 3624 Charm Bracelet
- 谷歌开源机器学习,这个狂拽炫酷吊炸天的技术究竟是什么玩意?
- NOJ1142-最大连续和
- 2016.4.10Python列表生成式及生成器generator
- LeetCode(java)5. Longest Palindromic Substring
- CSS中的负margin之我见
- SQLSever安装出错