class Solution {
public int findKthLargest(int[] nums, int k) {
//钻取最大最小
int min=nums[0];
int max=nums[0];
for(int i:nums){//遍历
if(i<min){
min=i;//记录最小
}
if(i>max){
max=i;//记录最大
}
}
int [] count=new int[max-min+1];//区间
for(int i:nums){
count[i-min]++;;//每个数出现的次数
}
for(int i=max-min;i>=0;i--){//从后往前
k-=count[i];
if(k<=0){
return i+min;//返回第K个
}
}
return nums[0];
}
}
LeetCode215题数组中的第K个最大元素
最新推荐文章于 2024-05-20 18:16:07 发布