数组中的第K大元素
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4
class Solution(object):
def findKthLargest(self, nums, k):
# 方法一:这里使用了自建堆,可以使用[0]等索引方法
heap = [x for x in nums[:k]]
heapq.heapify(heap)
for i in range(k, len(nums)):
if nums[i] > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, nums[i])
return heap[0]
class Solution(object):
def findKthLargest(self, nums, k):
# 方法二:堆的另一种方法,常用
heap = []
for i in nums:
heapq.heappush(heap, i)
if len(heap) > k:
heapq.heappop(heap)
return heap[0]
class Solution(object):
def findKthLargest(self, nums, k):
# 方法三:快排,但不是最优解
# 第k个最大的元素,即升序排列后,index为len(nums)-k
k = len(nums) - k
low = 0
high = len(nums) - 1
while low <= high:
p = self.sort_nums(nums, low, high)
if k < p:
high = p-1
elif k > p:
low = p+1
else:
return nums[p]
return -1
def sort_nums(self, alist, low, high):
mid_value = alist[low]
while low <high:
while low < high and alist[high] >= mid_value:
high -= 1
alist[low] = alist[high]
while low < high and alist[low] <= mid_value:
low += 1
alist[high] = alist[low]
alist[low] = mid_value
return low
# Go语言版
func findKthLargest(nums []int, k int) int {
return sort_nums(nums,k)
}
func sort_nums(nums []int, k int) int { //快排
i := 0
j := len(nums) - 1
pivot := nums[0]
for i < j{
for i < j && nums[j] < pivot{
j--
}
if i < j{
nums[i], nums[j] = nums[j], nums[i]
i++
}
for i < j && nums[i] > pivot{
i++
}
if i < j{
nums[i], nums[j] = nums[j], nums[i]
j--
}
}
if i+1 == k{
return pivot // 位子恰好就是K-1返回结果
}else if i+1 < k{
return sort_nums(nums[i+1:], k-i-1) //以pivot为断点,在后半部分数组中进行查找
}else{
return sort_nums(nums[:i], k) //在前半部分数组中进行查找
}
}
前K个高频元素
给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
输入: nums = [1], k = 1
输出: [1]
class Solution(object):
def topKFrequent(self, nums, k):
dicts = {}
res = []
for i in range(len(nums)):
if nums[i] in dicts:
dicts[nums[i]] += 1
else:
dicts[nums[i]] = 1
for key, value in dicts.items():
heapq.heappush(res, (value, key))
if len(res) > k:
heapq.heappop(res)
return [x[-1] for x in res]
前K个高频单词
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
输入: [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2
输出: [“i”, “love”]
解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 “i” 在 “love” 之前。
输入: [“the”, “day”, “is”, “sunny”, “the”, “the”, “the”, “sunny”, “is”, “is”], k = 4
输出: [“the”, “is”, “sunny”, “day”]
解析: “the”, “is”, “sunny” 和 “day” 是出现次数最多的四个单词,
出现次数依次为 4, 3, 2 和 1 次。
class Solution(object):
def topKFrequent(self, words, k):
dicts = {}
res = []
for i in range(len(words)):
if words[i] not in dicts:
dicts[words[i]] = 1
else:
dicts[words[i]] += 1
for key, value in dicts.items():
heapq.heappush(res, (-value, key))
tmp = []
for _ in range(k):
tmp.append(heapq.heappop(res)[-1])
return tmp
根据字符出现频率排序
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
输入:
“tree”
输出:
“eert”
解释:
‘e’出现两次,’r’和’t’都只出现一次。
因此’e’必须出现在’r’和’t’之前。此外,”eetr”也是一个有效的答案。
输入:
“cccaaa”
输出:
“cccaaa”
解释:
‘c’和’a’都出现三次。此外,”aaaccc”也是有效的答案。
注意”cacaca”是不正确的,因为相同的字母必须放在一起。
class Solution(object):
def frequencySort(self, s):
dicts = {}
lists = []
res = ''
# 计数
for i in range(len(s)):
if s[i] in dicts:
dicts[s[i]] += 1
else:
dicts[s[i]] = 1
# 根据次数使用堆排序
for key, value in dicts.items():
heapq.heappush(lists, (value, key))
# 拼凑字符串
while lists:
tmp = heapq.heappop(lists)
res = tmp[0]*tmp[-1] + res
return res