-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_k.py
68 lines (61 loc) · 1.73 KB
/
top_k.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
68
# 查找数组中第k大的数
import random
#快速排序方式,不考虑重复的数字
def top_k(nums, begin, end, k):
l = begin
r = end
key = nums[l]
while (l < r):
while (l < r and nums[r] <= key): #从大到小排序
r -= 1
nums[l] = nums[r]
while (l < r and nums[l] >= key):
l += 1
nums[r] = nums[l]
nums[l] = key
if l == (k - 1):
return nums[l]
elif l > (k - 1):
return top_k(nums, begin, l - 1, k)
else:
return top_k(nums, l + 1, end, k)
#nums = [7, 9, 5, 9, 2, 0, 7, 3, 3, 1]
#print(nums)
#print(top_k(nums,0,len(nums)-1, 3))
for i in range(100000): #循环多次测试
nums = [random.randint(0, 10) for _ in range(10)]
copy = nums.copy()
copy.sort()
if copy[-3] == top_k(nums,0,len(nums)-1, 3):
#print('bingo')
pass
else:
print(nums)
print(top_k(nums,0,len(nums)-1, 3))
def top_3(nums):
if len(nums)<3:
return -1
k = [-pow(2,15) for _ in range(3)] #chush初始化
for num in nums:
if num > k[0]: #先把k往后滚动一格,再把最大的k[0]重新赋值为当前num
k[1:3] = k[0:2]
k[0] = num
elif num < k[0] and num > k[1]:
k[2] = k[1]
k[1] = num
elif num < k[1] and num > k[2]:
k[2] = num
return k[2]
#nums = [6, 6, 8, 2, 1, 6, 5, 3, 4, 1]
#print(top_3(nums))
for i in range(100000): #多次测试
nums = [random.randint(0, 10) for _ in range(10)]
copy = list(set(nums.copy()))#set去重并乱序
copy.sort()
if copy[-3] == top_3(nums):
#print('bingo')
pass
else:
print('nums',nums)
print('copy',copy)
print(top_3(nums))