LeetCode 215. Kth Largest Element in an Array(排序)

题目

题意:找到一个数组里第K大的数字。

题解:我们当然可以排序好了,之后,选择第K大的数字。但是这样做一点技术含量也没有。

       排序算法选用快排。寻找第K大的数字,不必把数组完全排完序之后,再找第K大。快排中是选取一个数字,把大于它的放在右边,小于它的放在左边,在递归的时候,我们判断k 和右边数字的个数,如果k大,说明第k大的数字在左边,于是我们去找左边的部分,就不用递归排序右边的部分了,如果k小。

快排的平均效率是O(nlog(n)) 而,这道题目用快排的效率就是O(n)的效率,实际上是O((1-1/2^n)n) 当n趋近于无穷大的时候,就是O(n)。

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {

          return fun(nums,0,nums.size()-1,k);

    }

    int fun(vector<int>& nums,int l,int r,int k)
    {
        int s = l+1;
        int e = r;

        while(s<=e)
        {
            while(e>=s&&nums[e]>nums[l])
            {
                e--;
            }

            while(s<=e&&nums[s]<nums[l])
            {
                s++;
            }
            if(s<=e)
            {
                swap(nums[e],nums[s]);
                e--;
                s++;
            }

        }

        swap(nums[l],nums[e]);

        int x = r - e;
        if(x==k-1)
            return nums[e];
        else if(x>k-1)
            return fun(nums,e+1,r,k);
        else
            return fun(nums,l,e-1,k-x-1);

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12344804.html

时间: 2024-10-10 12:31:08

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

网易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 215 : 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 is always valid, 1 ≤ k ≤ array's

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

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. 解题思路: 本题是<算法导论>原题,

leetcode 215. Kth Largest Element in an Array 寻找第k个大的数---------- java

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's

leetcode 215 Kth Largest Element in an Array : 快排

在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素. 注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字. 解法一:直接将数组从大到小排序,取出下标为n-k的元素. class Solution { public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); return nums[nums.si

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:

刷题215. Kth Largest Element in an Array

一.题目说明 题目215. Kth Largest Element in an Array,在一个无序数组中找第k大的元素.难度是Medium! 二.我的解答 这个题目最直观的解答是,先对数组排序,然后直接返回: class Solution{ public: int findKthLargest(vector<int>& nums,int k){ sort(nums.begin(),nums.end()); return nums[nums.size()-k]; } }; 性能如下:

【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