-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17leet692.py
More file actions
50 lines (45 loc) · 1.38 KB
/
17leet692.py
File metadata and controls
50 lines (45 loc) · 1.38 KB
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
# Python TR
# Time:2021/8/20 1:24 下午
# coding = utf-8
class Solution(object):
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
dict = {}
lenList = []
res = []
findLen = len(words)-k
for i,word in enumerate(words):
dict[len(word)] = i
lenList.append(len(word))
self.quickSore(lenList,0,len(lenList)-1,findLen)
for i in range(findLen,len(lenList)):
res.append(words[dict[lenList[i]]])
return res
def quickSore(self,nums,low,high,findLen):
idx = self.partition(nums, low, high)
if idx<findLen:
self.quickSore(nums,idx+1,high)
else:
self.quickSore(nums,low,idx-1)
self.quickSore(nums,idx+1,high)
def partition(self,nums,low,high):
if nums==None or len(nums)==0:
return None
temp = nums[low]
while low<high:
while low<high and temp<nums[high]:
high-=1
nums[high] = nums[low]
while low<high and nums[low]<temp:
low+=1
nums[low] = nums[high]
nums[low] = temp
return low
if __name__ == '__main__':
words = ["i", "love", "leetcode", "i", "love", "coding"]
k=2
topKFrequent(object,words,k)