algorithm notes: leetcode#280 wiggle sort

Problem


Analysis


Solution


1
2
3
4
5
6
7
8
9
10
11
class :
def wiggleSort(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
tmp = sorted(nums)
for p, i in enumerate(range(0, len(nums), 2)):
nums[i] = tmp[p]
for p, i in enumerate(range(1, len(nums), 2)):
nums[i] = tmp[-1-p]
1
2
3
4
5
6
7
8
class :
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):
nums[i:i+2] = sorted(nums[i:i+2], reverse=i%2)

280. Wiggle Sort