189. Rotate Array
来源:互联网 发布:ospf使用的算法 编辑:程序博客网 时间:2024/06/07 13:05
题目来源【Leetcode】
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].
方法一:往前插入
class Solution {public: void rotate(vector<int>& nums, int k) { if(k != 0 && nums.size()!=0){ int i = 0; while(i < k){ nums.insert(nums.begin(),nums[nums.size()-1]); nums.pop_back(); i++; } } }};
方法二:直接进行移动:
class Solution {public: void rotate(vector<int>& nums, int k) { int n = nums.size(); if(k > 0 && n !=0){ vector<int>temp(nums); for(int i = 0; i < n; i++){ nums[(i+k)%n] = temp[i]; } } }};
阅读全文
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
- 你还爱我吗????
- myeclipse中xml读取xsd失败----- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaratio
- Linux常用命令整理
- mysqli防SQL注入(代码方面)
- c:foreach,c:if循环控制每行显示的个数
- 189. Rotate Array
- 六、教程篇-《http get无参数和有参数请求》
- android 自定义处理全局异常
- stringify的问题
- Device Tree:代码分析
- 博客测试
- 2588: Spoj 10628. Count on a tree
- 还在为画“类Word文档报表”而发愁吗?
- 【DFS】poj 1979 Red and Black