Why test case [1,2],1 works on my eclipse but failed on LeetCode?


  • 0
    S

    Here is my code:

    int len = nums.length;
    	int[] res = new int[len];
    	k %= len;
        for (int i = 0; i < k; i ++) {
        	res[i] = nums[len -k + i];
        }
        
        for (int i = 0; i < len - k; i ++) {
        	res[i+k] = nums[i];
        }
        
        nums = res;
    

    I tested on my computer, it does return [2,1]. Why I submit on LeetCode it says that my answer is [1,2]??


  • 0
    S
    for (int i = 0; i < len; i ++) {
            nums[i] = res[i];
        }
    

    I change the assignment to this and it works. I think maybe LeetCode check that nums refers to at the beginning. With "=" assignment, it only make nums refer to something else but the data that nums referred before remain the same. That's why it failed on LeetCode


Log in to reply
 

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