Simple, short solution:)


  • 0
    S
    public class Solution {
        public void wiggleSort(int[] nums) {
           if(nums == null || nums.length == 0) {
               return;
           } 
           
           for(int i=1; i<nums.length; i++) {
               int pre = nums[i-1];
               if((i%2 == 1 && pre > nums[i]) || (i%2 == 0 && pre< nums[i])){
                   swap(nums, i);
               }
           }
        }
        
        
        public void swap(int[] nums, int index) {
            int pre = nums[index-1];
            nums[index-1] = nums[index];
            nums[index] = pre;
        }
    }

Log in to reply
 

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