Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
class Solution {
public:
void rotate(vector<int>& nums, int k) {
std::rotate(rbegin(nums), rbegin(nums) + k % size(nums), rend(nums));
}
};
1234 567 => 目标 567 1234
765 4321
567 1234
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k %= size(nums);
reverse(begin(nums), end(nums));
reverse(begin(nums), begin(nums) + k);
reverse(begin(nums) + k, end(nums));
}
};