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

  • 0

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

    class Solution {
        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]);

Log in to reply

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