-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path280_Wiggle_Sort.py
40 lines (37 loc) · 1.34 KB
/
280_Wiggle_Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution(object):
# def wiggleSort(self, nums):
# """
# :type nums: List[int]
# :rtype: void Do not return anything, modify nums in-place instead.
# """
# # Sort
# if nums is None or len(nums) == 0:
# return
# nums.sort()
# # n - x - 1 = x
# wiggle = (len(nums) - 1) / 2
# for i in range(wiggle):
# pos = i * 2 + 1
# nums.insert(pos, nums.pop(-1))
# def wiggleSort(self, nums):
# if nums is None or len(nums) == 0:
# return
# nums.sort()
# for i in range(1, len(nums) - 1, 2):
# # swap i and i + 1
# nums[i], nums[i + 1] = nums[i + 1], nums[i]
# def wiggleSort(self, nums):
# less = True
# for i in range(len(nums) - 1):
# if less:
# if nums[i] > nums[i + 1]:
# nums[i], nums[i + 1] = nums[i + 1], nums[i]
# else:
# if nums[i] < nums[i + 1]:
# nums[i], nums[i + 1] = nums[i + 1], nums[i]
# less = not less
def wiggleSort(self, nums):
for i in range(len(nums) - 1):
if (i % 2 == 0 and nums[i] > nums[i + 1]) or\
(i % 2 == 1 and nums[i] < nums[i + 1]):
nums[i], nums[i + 1] = nums[i + 1], nums[i]