超时
package main
import "fmt"
func findKthLargest(nums []int, k int) int {
left := 0
right := len(nums)-1
length :=len(nums)-k
for{
if left == right{// 循环终止条件
return nums[right]
}
p := partition(nums,left,right)
if p == length{
return nums[p]
}else if p<length{
left = p+1
}else{
right = p-1
}
}
}
func partition(nums []int,left int,right int)(pos int){
x := nums[left]
i := left+1
j := right
for i<=j{
for nums[j]>=x && j>left{
j = j-1
}
for nums[i]<=x && i<right{
i = i+1
}
if i<j{
temp := nums[i]
nums[i],nums[j] = nums[j],temp
i = i+1
j = j-1
}
}
t := nums[left]
nums[left],nums[j] = nums[j],t
return j
}
func main(){
nums := []int{3,2,1,5,6,4}
fmt.Println(findKthLargest(nums,3))
}