Wiggle Sort II

Problem: Wiggle Sort II

We need first sort all the numbers and assign smaller half of numbers to positions with even indexes and bigger half to positions with odd indexes.

Code in Python:

class Solution(object):
    def wiggleSort(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        nums.sort()
        m = len(nums)/2 if len(nums)%2 else len(nums)/2-1
        nums[::2], nums[1::2] = nums[m::-1], nums[:m:-1]

results matching ""

    No results matching ""