刷题记录(Kth Largest Element in an Array)
程序员文章站
2022-04-25 11:45:29
...
Leecode: Kth Largest Element in an Array(JS解答)
解题思路:先通过快排排序,然后从后向前遍历,获取所求值,快排解法可以去本人博客算法一栏中找到相应介绍
function findKthLargest(nums: number[], k: number): number {
quickSort(nums,0,nums.length-1);
for(var i=nums.length-1; i >=0;i--){
k--;
if(k == 0) {
return nums[i];
}
}
};
function quickSort(arrs:number[],start:number,end:number) {
if(start > end) return;
var i:number = start;
var j:number = end;
var pivot:number = arrs[start];
while(i != j){
while(arrs[j] >= pivot && j > i) j--;
while(arrs[i] <= pivot && i < j) i++;
if(i < j){
swaper(i,j,arrs);
}
}
arrs[start] = arrs[i];
arrs[i] = pivot;
quickSort(arrs,start,i-1);
quickSort(arrs,i+1,end);
return;
}
function swaper(i:number,j:number,arr:number[]) {
var temp:number = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
上一篇: 前端面试知识点总结(持续更新)一
下一篇: Maven快速上手
推荐阅读
-
Kth Largest Element in an Array Leetcode #215 题解[C++]
-
215. Kth Largest Element in an Array
-
LeetCode 215 Kth Largest Element in an Array
-
LeetCode 215. Kth Largest Element in an Array
-
LeetCode算法问题14 —— Kth Largest Element in an Array
-
215. Kth Largest Element in an Array(返回数组中第几大元素)(leetcode)
-
LeetCode 215 -- 数组中的第K个最大元素 ( Kth Largest Element in an Array ) ( C语言版 )
-
Kth Largest Element in an Array解题报告
-
215[Medium]:Kth Largest Element in an Array
-
leetcode215:Kth Largest Element in an Array