-
Notifications
You must be signed in to change notification settings - Fork 0
/
75.颜色分类.py
68 lines (60 loc) · 1.69 KB
/
75.颜色分类.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# @lc app=leetcode.cn id=75 lang=python3
#
# [75] 颜色分类
#
# @lc code=start
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# 传统解法
count = [0 for _ in range(3)]
for i in nums:
count[i] += 1
start_index = 0
for i in range(3):
for j in range(count[i]):
nums[start_index+j] = i
start_index+=count[i]
# 两趟扫描
n = len(nums)
ptr = 0
for i in range(n):
if nums[i] == 0:
nums[i], nums[ptr] = nums[ptr], nums[i]
ptr += 1
for i in range(ptr, n):
if nums[i] == 1:
nums[i], nums[ptr] = nums[ptr], nums[i]
ptr += 1
# 一趟扫描
n = len(nums)
p0 = p1 = 0
for i in range(n):
if nums[i] == 1:
nums[i], nums[p1] = nums[p1], nums[i]
p1 += 1
elif nums[i] == 0:
nums[i], nums[p0] = nums[p0], nums[i]
if p0 < p1:
nums[i], nums[p1] = nums[p1], nums[i]
p0 += 1
p1 += 1
#三指针解法
l,r=0,len(nums)-1
cur=0
while cur<=r:
if nums[cur]==0:
if nums[l]!=0:
nums[cur],nums[l]=nums[l],nums[cur]
l+=1
cur+=1
elif nums[cur]==2:
if nums[r]!=2:
nums[r],nums[cur]=nums[cur],nums[r]
r-=1
else:
cur+=1
# @lc code=end