Why does the test fail on this code?


  • 0
    N

    void rotate(int nums[], int n, int k) {
    int * temp = new int[n];
    int index = k+1; int i = 0;
    while(i < n)
    {
    temp[i++] = nums[index++];
    index = (index%n);
    }
    nums = temp;
    for(int i =0; i <n;i++) cout<<nums[i]<<" ";
    delete []temp;
    }


  • 0
    L

    Be careful, variable nums is just pointer.
    The sentence(nums = temp) doesn't actually modify the real address of data.


Log in to reply
 

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