Why my java simple code is showing error


  • 0
    publicvoid rotate(int[] nums, int k) {
    		if(nums.length<k) k%=nums.length;
    		int[] temp=new int[nums.length];
    		for(int i=0;i<k;i++)
    			temp[i]=nums[nums.length-k+i];
    		for(int i=k;i<nums.length;i++)
    			temp[i]=nums[i-k];
    	}
    

    System says Input set as "[1,2], 1" and the result was [1,2], while I runned myeclipese I got [2,1], where is the problem occurs?


  • 0

    Well, you're supposed to modify the given array, and you're not doing that at all. You create a new array and the OJ doesn't care about it. I note you changed the return type from void to int[]. That should've been your first hint. Don't do that.


  • 0

    Thanks for your gentle reminder,I‘ve correct this mistake, but the problem still remaining....
    my code is amended as it shown in question box
    By the way, I'm confused with the fact that if the return type is void, how could OJ get my rotate result?


  • 0

    how could OJ get my rotate result?

    Like I said: you're supposed to modify the given array. The OJ will then simply look at that array again.


  • 0

    I'm sorry for ignoring this mistake。。。。I‘ve correct it,and get accepted..Thanks for your gentle reminder!!


Log in to reply
 

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