Please help to check!!! It is correct in my environment while it shows wrong in OJ


  • 0
    M
        int n = nums.length;
        LinkedList<Integer> queue = new LinkedList<Integer>();
        for(int i = 0; i < n; i++){
        	queue.add(nums[i]);
        }
        
        for(int i = 0; i < k; i++){
        	Integer last = queue.removeLast();
        	queue.addFirst(last);
        }
        Integer[] nums1 = (Integer[])queue.toArray(new Integer[queue.size()]);
        int[] num_new = new int[n];
        for(int i = 0; i < n; i++){
        	num_new[i] = nums1[i].intValue();
        }
        nums = num_new;

  • 0

    The last line nums = num_new means change 'nums' reference to 'num_new'. after function return it does not truly change values in nums (references are passed by value).

    May be you should use loop:

    for(int i = 0; i < num_new.length; i++){
         nums[i] = num_new[i];
    }
    

    And it is better use (k % nums.length) instead of k. If k is a big value greater than nums.length, It is easy case timeout problem.


Log in to reply
 

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