Simple method in c program


  • 0
    Z
    void revert_arr(int *arr, int n) {
        int l = 0;
        int h = n-1;
        int tmp;
        
        while (l < h) {
            tmp = arr[l];
            arr[l] = arr[h];
            arr[h] = tmp;
            l++; h--;
        }
    }
    
    void rotate(int* nums, int numsSize, int k) {
        k = k % numsSize;
        revert_arr(nums, numsSize-k);
        revert_arr(nums+(numsSize-k), k);
        revert_arr(nums, numsSize);
    }
    

    This algorithm is in programming pearls. Good book.


Log in to reply
 

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