题解 | #寻找第K大#
寻找第K大
https://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param a int整型一维数组
# @param n int整型
# @param K int整型
# @return int整型
#
from typing import List
class Solution:
def findKth(self , a: List[int], n: int, K: int) -> int:
# write code here
def quickSort(l: int, r: int, k: int) -> int:
if l >= r:
return a[l]
i, j = l - 1, r + 1
x = a[l + (r - l) // 2]
while i < j:
while True:
i += 1
if a[i] >= x:
break
while True:
j -= 1
if a[j] <= x:
break
if i < j:
a[i], a[j] = a[j], a[i]
s1 = j - l + 1
if k <= s1:
return quickSort(l, j, k)
else:
return quickSort(j + 1, r, k - s1)
return quickSort(0, n - 1, n - K + 1)
算法刷题记录 文章被收录于专栏
刷题,记录牛客的101
百融云创晋升空间 23人发布