-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk-th.py
54 lines (46 loc) · 1021 Bytes
/
k-th.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2019-06-21 15:10:56
# @Author : Kelly Hwong ([email protected])
# @Link : http://example.org
# @Version : $Id$
import os
def k_th(nums: list, k: int):
'''
input:
nums: N 个数
k: 第 k 个
output:
第 k 大的数
'''
# 排序法,这样就变成了排序问题
# 排序法有很多种
'''
nums.sort()
print(nums)
'''
# 计数法,用字典
d = dict()
for _ in nums:
if _ not in d:
d[_] = 1
else:
d[_] += 1
# 得到了每个数有多少个
max_num = max(d.keys())
'''
for _ in reversed(range(max_num+1)):
if _ in d.keys():
d[]
'''
return nums[k-1]
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
return nums[:k-1]
def main():
nums = [10, 30, 5, 7, 12, 20, 3, 3, 10]
k = 3
print(k_th(nums, k))
if __name__ == "__main__":
main()