LeetCode Kth Largest Element in an Array (快速排序)

题意:

  在一个无序的数组中第k大的数是多少?

思路:

  按照快排的思路,如果每次分成两段后,设为L和R。如果R>=k ,则答案在右边集合,否则在左边集合。

  这里用了3位取中法。注意快排别给写死循环了。

 1 class Solution {
 2 public:
 3     int findKthLargest(vector<int>& nums, int k) {
 4         if(k>nums.size())    return 0;
 5            return DFS(nums,0,nums.size()-1,k);
 6     }
 7
 8     int DFS(vector<int>& nums,int s,int e,int k)
 9     {
10         int L=s, R=e;
11         //三位取中法
12         if(nums[e]>nums[s])    swap(nums[s],nums[e]);
13         if(nums[s]>nums[(s+e)/2])    swap(nums[s],nums[(s+e)/2]);
14
15         int mid=nums[s];
16         while(L<R)
17         {
18             while(L<R && nums[R]>=mid)    R--;    //找小
19             nums[L]=nums[R];
20             while(L<R && nums[L]<=mid)    L++;    //找大
21             nums[R]=nums[L];
22         }
23         nums[L]=mid;
24         int len=e-L;//右边部分的元素个数
25         if(len+1==k)    return mid;
26         if(len>=k)    return DFS(nums,L+1,e,k);
27         else        return DFS(nums,s,L-1,k-len-1);
28     }
29 };

AC代码

  快排:

 1     void quick(vector<int>& nums,int s,int e)
 2     {
 3         int L=s, R=e;
 4         //三位取中法
 5         if(nums[e]>nums[s])            swap(nums[s],nums[e]);    //求大
 6         if(nums[s]>nums[(s+e)/2])    swap(nums[s],nums[(s+e)/2]); //求中
 7         int pivot=nums[s];
 8         while(L<R)
 9         {
10             while(L<R && nums[R]>=pivot)    R--;    //找小
11             nums[L]=nums[R];
12
13             while(L<R && nums[L]<=pivot)    L++;    //找大
14             nums[R]=nums[L];
15         }
16         nums[L]=pivot;
17         quick(nums,s,L-1);
18         quick(nums,L+1,e);
19     }

AC代码

时间: 2024-10-19 00:34:23

LeetCode Kth Largest Element in an Array (快速排序)的相关文章

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

LeetCode——Kth Largest Element in an Array

Description: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. 你确定你不是在逗我? public class Solution { public

LeetCode:Kth Largest Element in an Array(need update)

problem: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

Leetcode题解(3):L215/Kth Largest Element in an Array

L215: Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may as

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k