本文主要是介绍LeetCode *** 189. Rotate Array,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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) {int len=nums.size();for(int i=0;i<k;++i){int tmp=nums[len-1];nums.erase(nums.end()-1);nums.insert(nums.begin(),tmp);}}
};
这篇关于LeetCode *** 189. Rotate Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!