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

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐++215. 数组中的第K个最大元素++------题解思路](#⭐215. 数组中的第K个最大元素——题解思路)
  • [3- ACM实现](#3- ACM实现)


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 定义 lege 下标 使用 while(true)
    • 使得 le 指向的元素始终小于 pivot
    • 使得 ge 指向的元素始终大于 pivot

2- 实现

⭐++215. 数组中的第K个最大元素++------题解思路

java 复制代码
import java.util.Random;
class Solution {


    static Random random = new Random(System.currentTimeMillis());
    public int findKthLargest(int[] nums,int k){
        return quickSelect(nums,0,nums.length-1,nums.length-k);

    }

    public int quickSelect(int[] nums,int left,int right,int kIndex){
        if(right==left){
            return nums[left];
        }
        //
        int pivotIndex = partition(nums,left,right);

        if(pivotIndex == kIndex){
            return nums[kIndex];
        }else if( pivotIndex>kIndex){
            return quickSelect(nums,left,pivotIndex-1,kIndex);
        }else{
            return quickSelect(nums,pivotIndex+1,right,kIndex);
        }
    }

    public int partition(int[] nums,int left,int right){
        int randomIndex = left + random.nextInt(right-left+1);
        swap(nums,left,randomIndex);

        int mid = nums[left];
        int le = left+1;
        int ge = right;
        while(true){
            while(le<=ge && nums[le] < mid){
                le++;
            }
            while(le<=ge && nums[ge] > mid){
                ge--;
            }
            if(le>=ge){
                break;
            }
            swap(nums,le,ge);
            le++;
            ge--;
        }
        swap(nums,left,ge);
        return ge;
    }

    public void swap(int[] nums,int left,int right){
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }


}

3- ACM实现

java 复制代码
public class kthNums {

    static Random random = new Random(System.currentTimeMillis());
    public static int findK(int[] nums,int k){
        // 快速选择 ,传四个参数
        return quickSelect(nums,0,nums.length-1,nums.length-k);

    }

    public static int quickSelect(int[] nums,int left,int right,int kIndex){
        if(right==left){
            return nums[left];
        }
        //
        int pivotIndex = partition(nums,left,right);

        if(pivotIndex == kIndex){
            return nums[kIndex];
        }else if( pivotIndex>kIndex){
            return quickSelect(nums,left,pivotIndex-1,kIndex);
        }else{
            return quickSelect(nums,pivotIndex+1,right,kIndex);
        }
    }

    public static int partition(int[] nums,int left,int right){
        int randomIndex = left + random.nextInt(right-left+1);
        swap(nums,left,randomIndex);

        int mid = nums[left];
        int le = left+1;
        int ge = right;
        while(true){
            while(le<=ge && nums[le] < mid){
                le++;
            }
            while(le<=ge && nums[ge] > mid){
                ge--;
            }
            if(le>=ge){
                break;
            }
            swap(nums,le,ge);
            le++;
            ge--;
        }
        swap(nums,left,ge);
        return ge;
    }

    public static void swap(int[] nums,int left,int right){
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] parts = input.split(" ");
        int[] nums = new int[parts.length];
        for(int i = 0 ; i < nums.length ; i++){
            nums[i] = Integer.parseInt(parts[i]);
        }
        System.out.println("输入K");
        int k = sc.nextInt();
        System.out.println("结果是"+findK(nums,k));

    }
}
相关推荐
kkeeper~3 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
wabs6664 小时前
关于贪心算法的一些自我总结【力扣45.跳跃游戏II】【灵感来源:代码随想录】
算法·贪心算法·复盘
2401_876964135 小时前
【湖北专升本】2026湖北专升本真题PDF+备考资料汇总
数据结构·人工智能·经验分享·深度学习·算法·计算机视觉
嗝o゚5 小时前
CANN GE 算子融合——融合算法与调度策略
算法·昇腾·cann·ge
小江的记录本5 小时前
【JVM虚拟机】垃圾回收GC:垃圾回收算法:标记-清除、标记-复制、标记-整理、分代收集(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·算法·安全·面试
Ulyanov7 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
数据科学小丫7 小时前
特征工程处理
人工智能·算法·机器学习
z落落7 小时前
C#参数区别
java·算法·c#
c238568 小时前
vector(下)
数据结构·算法
z落落8 小时前
C# 冒泡排序+选择排序 + Array.Sort 自定义排序
数据结构·算法