O(2n-k) time and O(k) space solution


  • 0
    W

    I know this is not optimal. but the code is neat and easy to understand.

    class Solution {
    public:
        void rotate(vector<int>& nums, int k) {
            int n = nums.size();
            k = k%n;
            for (int i = 0; i < n-k; ++i) nums.push_back(nums[i]);
            nums.erase(nums.begin(),nums.begin()+n-k);
        }
    };

Log in to reply
 

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