
Problem Description:
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].
题目大意:
把一个数组的后N个数移动到数组的最前面。
Solutions:
利用插入的方式从后往前依次插入数组最前端即可。
Code in C++:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
for(int i=0;i<k;i++)
{
nums.insert(nums.begin(),nums.back());
nums.erase(nums.begin()+nums.size()-1);
}
}
};




近期评论