leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)

原创 2016年08月28日 15:25:35

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.


这题用 O(nlogn) 是可以过的,也就是说你可以先排序再找,也可以用 heap,priority_queu 等数据结构。
当然,用 Quicksort 里的 Quick-select(也就是 partion)方法是可以做到 O(n) 的。(对于为啥 quick select 是 O(n) 的可以看


class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        return nums[nums.size() - k - 1];
    }
};
class Solution {
private:
    int partition(vector<int>& nums, int left, int right) {
        int pivot = nums[left];
        int l = left + 1, r = right;
        while (l <= r) {
            if (nums[l] < pivot && nums[r] > pivot){
                swap(nums[l++], nums[r--]);
            }
            if (nums[l] >= pivot) l++;
            if (nums[r] <= pivot) r--;
        }
        swap(nums[left], nums[r]);
        return r;
    }

public:
    int findKthLargest_qsort(vector<int>& nums, int k) {
        int left = 0, right = nums.size() - 1;
        while (true) {
            int pos = partition(nums, left, right);
            if (pos == k - 1){
                return nums[pos];
            }
            if (pos > k - 1) {
                right = pos - 1;
            }else{
                left = pos + 1;
            }
        }
    }

};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses

Kth Largest Element in an Array,Different Ways to Add Parentheses
  • dwt0317
  • dwt0317
  • 2017年01月12日 22:59
  • 96

【LeetCode】215. Kth Largest Element in an Array,基于Java和C++的解法

215. Kth Largest Element in an Array Total Accepted: 57859 Total Submissions: 171725 Difficulty: M...

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...

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 ...
  • fly_yr
  • fly_yr
  • 2015年12月08日 13:54
  • 832

LeetCode-215.Kth Largest Element in an Array

https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in an ...

【leetcode】215. Kth Largest Element in an Array

【leetcode】215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Not...

Leetcode 215. Kth Largest Element in an Array[medium]

题目: Find the kth largest element in an unsorted array. Note that it is the kth largest element in t...

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 so...

Leetcode 215 Kth Largest Element in an Array 数组中第k大的元素

在一个无须数组中找出第k大的元素。

Leetcode--Add to List 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 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)
举报原因:
原因补充:

(最多只允许输入30个字)