3 lines O(n) solution with Python


  • 0
    A
    class Solution(object):
        def wiggleSort(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            for i in range(len(nums) - 1):
                if (not i % 2 and nums[i] > nums[i + 1]) or (i % 2 and nums[i] < nums[i + 1]):
                    nums[i], nums[i + 1] = nums[i + 1], nums[i]
    

Log in to reply
 

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