6 line O(n) ruby solution


  • 0
    T
    def wiggle_sort(nums)
      (0...(nums.length-1)).each do |i|
        nums[i+1], nums[i] = nums[i], nums[i+1] if i%2 == 0 && nums[i] > nums[i+1]
        nums[i+1], nums[i] = nums[i], nums[i+1] if i%2 == 1 && nums[i] < nums[i+1]
      end
    end
    

Log in to reply
 

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