3-line C++ solution using insert and erase


  • 0
    S
    class Solution {
    public:
        void rotate(vector<int>& nums, int k) {
            k %= nums.size();
            nums.insert(nums.begin(), nums.end() - k, nums.end());
            nums.erase(nums.end() - k, nums.end());
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.