We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 311bb36 commit 58e827eCopy full SHA for 58e827e
57. 数组中的第K个最大元素.md
@@ -0,0 +1,32 @@
1
+***给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。***
2
+
3
+
4
5
+```
6
+class Solution:
7
+ def findKthLargest(self, nums: List[int], k: int) -> int:
8
9
+ def partition(nums, left, right):
10
+ pivot = nums[right]
11
+ i = left
12
+ for j in range(left, right+1):
13
+ if nums[j] < pivot:
14
+ nums[i], nums[j] = nums[j], nums[i]
15
+ i += 1
16
17
+ return i
18
19
+ n = len(nums)
20
+ left = 0
21
+ right = n-1
22
+ target = n-k
23
+ while True:
24
+ index = partition(nums, left, right)
25
+ if index == target:
26
+ return nums[index]
27
+ elif index < target:
28
+ left = index+1
29
+ else:
30
+ right = index-1
31
32
images/algo27.jpg
18.6 KB
0 commit comments