[LeetCode] 189. Rotate Array
来源:互联网 发布:淘宝怎么用手机号登陆 编辑:程序博客网 时间:2024/06/10 12:12
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.
[show hint]
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
class Solution {public: void rotate(vector<int>& nums, int k) { const int n = nums.size(); if ((k = k % n) == 0) return; vector<int> copied(nums); for (int i = 0; i < n; i++) nums[i] = copied[(n - k + i) % n]; }};
// 空间复杂度O(1)class Solution {public: void rotate(vector<int>& nums, int k) { int n = nums.size(); vector<int>::iterator it = nums.begin(); for (; k %= n; n -= k, it += k) { for (int i = 0; i < k; i++) swap(it[i], it[n - k + i]); } }};
阅读全文
0 0
- [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
- [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
- leetcode-189. Rotate Array
- Leetcode 189. Rotate Array
- leetcode 189. Rotate Array
- 【翻译】Airbnb JavaScript Style Guide
- 向正在运行的Linux应用程序注入代码
- React Native No bundle URL present 问题
- Redis简介、安装及常用配置
- SharedPreferences中commit和apply的区别
- [LeetCode] 189. Rotate Array
- JNI之初长成(一)
- 【STL】stable_sort !!!
- Kotlin语言学习笔记(1)
- Python变量函数
- 正则表达式的基本算法
- python脚本实现给定标注bbox,landmark在原图中显示人脸框,人脸关键点
- 最小比例
- Eclipse下导入外部jar包的3种方式