class Solution {
public:
void reverse(vector<int>& nums, int left, int right) {
while(left < right) {
swap(nums[left++], nums[right--]);
}
}
void rotate(vector<int>& nums, int k) {
k %= nums.size();
reverse(nums, 0, nums.size() - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.size() - 1);
}
};
轮转数组-力扣
于 2024-07-11 17:41:33 首次发布