Python O(n) solution


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

Log in to reply
 

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