"Leetcode-Interview algorithm classic-java implementation" "215-kth largest Element in an array (number of K in array)"

Source: Internet
Author: User

"215-kth largest Element in an array (number of k large in array)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original Question

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

Main Topic

Find the element of the K-Large from an unordered array. Note that the K-large after sorting, and not the K-non-repeating element can assume that K must be valid, 1≤k≤ array length

Thinking of solving problems

O (n) Solution: Fast selection (Quickselect) algorithm

Code Implementation

Algorithm implementation class

Import java.util.Collections; Public classSolution { Public int findkthlargest(int[] Nums,intK) {if(K <1|| Nums = =NULL|| Nums.length < K) {Throw NewIllegalArgumentException (); }returnFindkthlargest (Nums,0, Nums.length-1, k); } Public int findkthlargest(int[] Nums,intStartintEndintK) {//Center value        intPivot = Nums[start];intLo = start;inthi = end; while(Lo < HI) {//Move the number less than the center value to the left of the array             while(Lo < hi && Nums[hi] >= pivot)            {hi--; } Nums[lo] = Nums[hi];//Move the number greater than the center value to the right of the array             while(Lo < hi && Nums[lo] <= pivot)            {lo++;        } Nums[hi] = Nums[lo]; } Nums[lo] = pivot;//If you have found        if(End-lo +1= = k) {returnPivot }//K-large number at the right of Lo position        Else if(End-lo +1> k) {returnFindkthlargest (Nums, lo +1, end, K); }//K-large number at the left of the LO position        Else{//K (end-lo+1)            //(END-LO+1): Indicates the number of elements from the LO position to the end position, or the right half.            //The original K-Large becomes K (end-lo+1) Large            returnFindkthlargest (nums, Start, lo-1, K-(End-lo +1)); }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48046265"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "215-kth largest Element in an array (number of K in array)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.