My C++ Code: Easy to Understand


  • 4
    C
    class Solution {
    public:
        void rotate(int nums[], int n, int k) {
            if (k > n)
                k %= n;
            rotate_helper(nums, 0, n-k-1);
            rotate_helper(nums, n-k, n-1);
            rotate_helper(nums, 0, n-1);
        }
    
        void rotate_helper(int nums[], int start, int end) {
            while (start < end) {
                int t = nums[start];
                nums[start] = nums[end];
                nums[end] = t;
                start++;
                end--;
            }
        }
    };

Log in to reply
 

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