【Hot100】LeetCode—215. 数组中的第K个最大元素

[复制链接]
发表于 2026-1-26 14:43:02 | 显示全部楼层 |阅读模式



  • 原题毗连:215. 数组中的第K个最大元素

1- 思绪

快速选择



  • 第 k 大的元素的数组下标: int target = nums.length - k
1- 根据 partition 分割的区间来判断当前处理处罚方式


  • 如果返回的 int 即是 target 分析找到了,直接返回
  • 如果返回的 int 小于 target 分析要在当前区间的右侧探求,也就是 [pivotIndex+1,right]
  • 如果返回的 int 大于 target 分析要在当前区间的左侧探求,也就是 [left,pivotIndex-1]
2- 实现 partition 随机选取一个 pivotIndex 分割区间


  • 2-1 随机选择一个下标
  • 2-2 互换 left 和 随机下标
  • 2-3 将随机下标的元素值设置为 pivot
  • 2-4 界说 le、ge 下标 使用 while(true)

    • 使得 le 指向的元素始终小于 pivot
    • 使得 ge 指向的元素始终大于 pivot


2- 实现

215. 数组中的第K个最大元素——题解思绪


  1. import java.util.Random;
  2. class Solution {
  3.     static Random random = new Random(System.currentTimeMillis());
  4.     public int findKthLargest(int[] nums,int k){
  5.         return quickSelect(nums,0,nums.length-1,nums.length-k);
  6.     }
  7.     public int quickSelect(int[] nums,int left,int right,int kIndex){
  8.         if(right==left){
  9.             return nums[left];
  10.         }
  11.         //
  12.         int pivotIndex = partition(nums,left,right);
  13.         if(pivotIndex == kIndex){
  14.             return nums[kIndex];
  15.         }else if( pivotIndex>kIndex){
  16.             return quickSelect(nums,left,pivotIndex-1,kIndex);
  17.         }else{
  18.             return quickSelect(nums,pivotIndex+1,right,kIndex);
  19.         }
  20.     }
  21.     public int partition(int[] nums,int left,int right){
  22.         int randomIndex = left + random.nextInt(right-left+1);
  23.         swap(nums,left,randomIndex);
  24.         int mid = nums[left];
  25.         int le = left+1;
  26.         int ge = right;
  27.         while(true){
  28.             while(le<=ge && nums[le] < mid){
  29.                 le++;
  30.             }
  31.             while(le<=ge && nums[ge] > mid){
  32.                 ge--;
  33.             }
  34.             if(le>=ge){
  35.                 break;
  36.             }
  37.             swap(nums,le,ge);
  38.             le++;
  39.             ge--;
  40.         }
  41.         swap(nums,left,ge);
  42.         return ge;
  43.     }
  44.     public void swap(int[] nums,int left,int right){
  45.         int tmp = nums[left];
  46.         nums[left] = nums[right];
  47.         nums[right] = tmp;
  48.     }
  49. }
复制代码

3- ACM实现

  1. public class kthNums {
  2.     static Random random = new Random(System.currentTimeMillis());
  3.     public static int findK(int[] nums,int k){
  4.         // 快速选择 ,传四个参数
  5.         return quickSelect(nums,0,nums.length-1,nums.length-k);
  6.     }
  7.     public static int quickSelect(int[] nums,int left,int right,int kIndex){
  8.         if(right==left){
  9.             return nums[left];
  10.         }
  11.         //
  12.         int pivotIndex = partition(nums,left,right);
  13.         if(pivotIndex == kIndex){
  14.             return nums[kIndex];
  15.         }else if( pivotIndex>kIndex){
  16.             return quickSelect(nums,left,pivotIndex-1,kIndex);
  17.         }else{
  18.             return quickSelect(nums,pivotIndex+1,right,kIndex);
  19.         }
  20.     }
  21.     public static int partition(int[] nums,int left,int right){
  22.         int randomIndex = left + random.nextInt(right-left+1);
  23.         swap(nums,left,randomIndex);
  24.         int mid = nums[left];
  25.         int le = left+1;
  26.         int ge = right;
  27.         while(true){
  28.             while(le<=ge && nums[le] < mid){
  29.                 le++;
  30.             }
  31.             while(le<=ge && nums[ge] > mid){
  32.                 ge--;
  33.             }
  34.             if(le>=ge){
  35.                 break;
  36.             }
  37.             swap(nums,le,ge);
  38.             le++;
  39.             ge--;
  40.         }
  41.         swap(nums,left,ge);
  42.         return ge;
  43.     }
  44.     public static void swap(int[] nums,int left,int right){
  45.         int tmp = nums[left];
  46.         nums[left] = nums[right];
  47.         nums[right] = tmp;
  48.     }
  49.     public static void main(String[] args) {
  50.         Scanner sc = new Scanner(System.in);
  51.         String input = sc.nextLine();
  52.         String[] parts = input.split(" ");
  53.         int[] nums = new int[parts.length];
  54.         for(int i = 0 ; i < nums.length ; i++){
  55.             nums[i] = Integer.parseInt(parts[i]);
  56.         }
  57.         System.out.println("输入K");
  58.         int k = sc.nextInt();
  59.         System.out.println("结果是"+findK(nums,k));
  60.     }
  61. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表