189. Rotate Array
来源:互联网 发布:爬虫软件有哪些 编辑:程序博客网 时间:2024/05/01 01:47
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].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Related problem: Reverse Words in a String II
解答
三次翻转
第一次将整个数组反转,第二次将前k个元素翻转,第三次将剩下的元素翻转。
class Solution {public: void rotate(vector<int>& nums, int k) { if(nums.empty()) return; int n = nums.size(); if(k == n) return; if(k>n) k = k%n; reverse(nums.begin(),nums.end()); vector<int>::iterator rotatePoint = nums.begin()+k; reverse(nums.begin(),rotatePoint); reverse(rotatePoint,nums.end()); }};
0 0
- Leetcode - Array - 189. Rotate Array
- 189. Rotate Array leetcode(array)
- LeetCode-Array-189. Rotate Array
- [LeetCode]189.Rotate Array
- [Leetcode] 189.Rotate Array
- LeetCode 189. Rotate Array
- 【LeetCode】189.Rotate Array
- [leetcode] 189.Rotate Array
- 【leetcode】189. Rotate Array
- Leetcode 189. Rotate Array
- 189. Rotate Array LeetCode
- 189. Rotate Array
- [LeetCode]189. Rotate Array
- 189. Rotate Array
- leetcode-189.-Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- Mac安装ReactNative
- Toolbar修改高度后返回按钮不能居中的解决方法
- linux--初见
- c语言实现des加密
- gets和scanf函数初篇
- 189. Rotate Array
- LeetCode: Palindrome Pairs
- Qt程序打包成exe可执行文件图文教程
- Spring配置文件写法
- 共同学习Java源码--concurrent包黑科技--Executor和ExecutorService
- 电话面试2 20160325_1733_27min
- Android工具类——TimeUtils时间戳与时间字符串相互转换
- git版本控制管理的学习(一)——初步探索
- OS X El Capitan更新到10.11.4失败提示-未能验证“OS X El Capitan 更新 ”解决方案