leetcode---Rotate Array
2016-05-13 14:06
246 查看
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].
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].
class Solution { public: void reverse(vector<int>&nums, int l, int r) { while(l < r) { swap(nums[l++], nums[r--]); } } void rotate(vector<int>& nums, int k) { int n = nums.size(); if(k > n) { k = k % n; } n -= 1; reverse(nums, 0, n); reverse(nums, 0, k-1); reverse(nums, k, n); } };
相关文章推荐
- Activity切换动画
- 启动Hbase,HMaster进程可以启动,但是HRegionServer启动不成功
- Nginx学习笔记一环境配置
- 【opencv】神经网络识别美女
- 负反馈电路放大倍数_基本放大倍数_反馈系数的公式
- SpringAOP面向切面详解(带实例)
- 当内容高度不定 ,而始终保持内容居中
- sql server mdf碎片级数据库修复,数据库碎片级提取
- Android 国际化语言
- Xbox360 十年祭:那些荣耀、激昂、内乱与未尽的未来
- 典型用户及用户场景描述
- 逻辑回归原理及实现
- 快递物流查询
- 一张图了解人气瀑布流网页设计
- 如何进行Android、IOS APP的自动化测试—东舟自动化测试解决方案
- unity3d格式的导出与加载
- Html.BeginForm与Ajax.BeginForm
- 还是老问题,出问题
- Android 异常日志捕捉Crash
- DN-BIT位