A Simple Java Solution. O(n) & 1ms.

  • 0

    Just located to an odd position number and compare its front and behind number.

    public class Solution {
        public void wiggleSort(int[] nums) {
            for(int i = 1;i<nums.length;i+=2){
                if(nums[i-1] > nums[i]) swap(nums,i);
                if( i<nums.length-1 && nums[i+1] > nums[i]) swap(nums,i+1);
        public void swap(int[] nums,int i){
            int temp = nums[i-1];
            nums[i-1] = nums[i];
            nums[i] = temp;

Log in to reply

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