clueless coding // TODO: be smarter

LeetCode Rotate Array: CPP Solution



TL;DR Code Solution



void reverseString(vector<int>& nums, int start, int end) {
    while (start < end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }


}

void rotate(vector<int>& nums, int k) {

    int n = (int)nums.size();
    k = k % n;

    if (n < 2) {
        return;
    }

    reverseString(nums, 0, n - k - 1);
    reverseString(nums, n - k, n - 1);
    reverseString(nums, 0, n - 1);

}