思路:partition函數(shù)
class Solution(object):
def findKthLargest(self, nums, k):
def partition(low,high):
pivot = nums[low]
while low<high:
while low<high and nums[high]>=pivot:
high-=1
nums[low] = nums[high]
while low<high and nums[low]<=pivot:
low+=1
nums[high]=nums[low]
nums[low] = pivot
return low
left = 0
right = len(nums) - 1
t = len(nums)-k+1
while True:
idx = partition(left, right)
if idx == t - 1:
return nums[idx]
if idx < t - 1:
left = idx + 1
if idx > t - 1:
right = idx - 1