【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 length.

思路:

依据时间和空间复杂度,有多种方法。这里给出易于实现,基本能够满足面试、笔试要求的类似于快速排序的(递归)实现。

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

        //以nums[0]作为分界元素,从nums[1]开始遍历
        //比nums[0]小的元素放入leftvec,反之放入rightvec
        for(int i = 1; i < nums.size(); i++)
        {
            if(nums[i] <= nums[0])
                leftvec.push_back(nums[i]);
            else
                rightvec.push_back(nums[i]);
        }

        //rightvec的长度即为大于nums[0]的元素的个数
        int len = rightvec.size();

        if(len >= k)//前k大的元素都在rightvec中,递归求解rightvec
        {
            return findKthLargest(rightvec, k);
        }
        else if(len == k - 1)//比nums[0]大的元素有k-1个,那么nums[0]即为所求结果
        {
            return nums[0];
        }
        else//比nums[0]大的元素有[0, k-2]个,说明第K大的元素在leftvec中,递归求解leftvec
        {
            return findKthLargest(leftvec, k - len - 1);
        }
    }
};    
时间: 2024-07-30 14:11:49

【LeetCode 215】Kth Largest Element in an Array的相关文章

【leetcode】Kth Largest Element in an Array (middle)☆

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. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

【leetcode】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

【LeetCode 230】Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 题意: 给定一个二分搜索树,返回第K小的结点 思路: 只要明白BST树的原理,只要中序遍历一遍BST树即可.求第K小的,只需遍历前K个结点就OK. C++: 1 /*

【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

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

网易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 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

刷题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]; } }; 性能如下: