Kth Largest Element II Lintcode - 璨璨要好好学习 - 博客园
Find K-th largest element in an array. and N is much larger than k.
Notice
You can swap elements in the array
Example
In array [9,3,2,4,8], the 3rd largest element is 4.
In array [1,2,3,4,5], the 1st largest element is 5, 2ndlargest element is 4, 3rd largest element is 3 and etc.
这道题可以用快速排序/快速选择的思想来做。
快排也写了不止一次了,但还是不够熟练。。。希望经过这次练习可以熟练一些。
需要注意的是注释的地方是不需要右边向左移动的,因为需要右边的指针停在第一个比pivot大的数的位置上,这样才可以调换。
Read full article from Kth Largest Element II Lintcode - 璨璨要好好学习 - 博客园
No comments:
Post a Comment