Maybe the simplest solution (java)


  • -2
    D
    public void wiggleSort(int[] nums) {
        Arrays.sort(nums); // time O(NlogN)
        if (nums.length <= 2)
            return;
        int[] tmp = new int[nums.length / 2]; // space O(N)
        System.arraycopy(nums, nums.length - nums.length / 2, tmp, 0, tmp.length);  //store the right half
        for (int i = nums.length - nums.length / 2 - 1; i >= 0; i--) {
            nums[i * 2] = nums[i]; // move the numbers of left half to their place 
        }
    
        for (int j = 0; j < tmp.length; j++) {
            nums[2 * j + 1] = tmp[j]; 
        }
    }

  • 0
    H

    I feel like it could work for Wiggle Sort but not for Wiggle Sort II. We don't want identical numbers to be adjacent.


Log in to reply
 

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