Why is wrong?input [1,2] output [1,2],but my eclipse output [2,1]


  • 0
    W
    public class Solution
    {
       public void rotate1(int[] nums, int k) 
        {
            int[] a = new int[nums.length] , b=new int[k];
            System.arraycopy(nums, 0, a, nums.length-(nums.length-k), nums.length-k);
            System.arraycopy(nums, nums.length-k, b, 0, k);
            System.arraycopy(b, 0, a, 0, b.length);
        }
    }
    

    That's run ok with my Eclipse, but wrong on this website.


  • 1
    T

    you should consider about a situation when k > nums.length . In that case: k = k%nums.length


  • 0
    W

    thanks, but when k>nums.length, this code will get an Exception of java.lang.ArrayIndexOutOfBoundsException, rather than get a valid output
    This is not the fundamental reason


Log in to reply
 

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