1 class Solution { 2 public: 3 int findKthLargest(vector<int>& nums, int k) { 4 int sz=nums.size(); 5 int find=sz-k; 6 int left=0,right=sz-1; 7 while(1){ 8 int cur=cut(nums, left, right); 9 if(cur==find) 10 return nums[cur]; 11 else if(cur<find) 12 left=cur+1; 13 else 14 right=cur-1; 15 } 16 } 17 18 int cut(vector<int>& nums, int start, int end){ 19 int temp=nums[start]; 20 int left=start; 21 int right=end; 22 while(left!=right){ 23 while(left<right&&nums[right]>temp) 24 --right; 25 while(left<right&&nums[left]<=temp) 26 ++left; 27 if(left<right) 28 swap(nums[left], nums[right]); 29 } 30 swap(nums[start], nums[left]); 31 return left; 32 } 33 };
就是用快排那种划分,每次确定一个最终元素的位置,如果该位置是要找的数,则返回