力扣labuladong——一刷day39

文章介绍了如何使用归并排序和快速排序解决LeetCode上的两个问题:912.排序数组和215.数组中的第K个最大元素,以及优先级队列在求解第K个最大元素中的应用。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


用一句话总结了归并排序:先把左半边数组排好序,再把右半边数组排好序,然后把两半数组合并。 同时我提了一个问题,让你一句话总结快速排序,这里说一下我的答案: 快速排序是先将一个元素排好序,然后再将剩下的元素排好序。

一、力扣912. 排序数组

class Solution {
    public int[] sortArray(int[] nums) {
        Quick.sort(nums);
        return nums;
    }
}
class Quick{
    public static void sort(int[] nums){
        shuffle(nums);
        quickSort(nums,0,nums.length-1);
    }
    public static void quickSort(int[] nums, int low, int high){
        if(low >= high){
            return;
        }
        int index = part(nums, low, high);
        quickSort(nums,low, index-1);
        quickSort(nums,index+1,high);
    }
    public static int part(int[] nums, int low, int high){
        int flag = nums[low];
        while(low < high){
            while(low<high && nums[high] >= flag)high--;
            nums[low] = nums[high];
            while(low<high && nums[low] <= flag)low++;
            nums[high] = nums[low];
        }
        nums[low] = flag;
        return low;
    }
    public static void shuffle(int[] nums){
        Random rand = new Random();
        for(int i = 0; i < nums.length; i ++){
            int r = i + rand.nextInt(nums.length-i);
            swap(nums,i,r);
        }
    }
    public static void swap(int[] nums,int low, int high){
        int temp = nums[low];
        nums[low] = nums[high];
        nums[high] = temp;
    }
}

二、力扣215. 数组中的第K个最大元素

优先级队列

class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int a : nums){
            pq.offer(a);
            if(pq.size() > k){
                pq.poll();
            }
        }
        return pq.peek();
    }
}

快排

class Solution {
    public int findKthLargest(int[] nums, int k) {
        fullss(nums,0,nums.length-1);
        quickSort(nums,0, nums.length-1,nums.length-k);
        return nums[nums.length-k];
    }
    public void quickSort(int[] nums ,int low, int high,int k){
        if(low >= high){
            return;
        }
        int index = part(nums,low,high);
        quickSort(nums,low,index-1,k);
        quickSort(nums,index+1,high,k);
    }
    public int part(int[] nums, int low, int high){
        int flag = nums[low];
        while(low < high){
            while(low<high && nums[high] >= flag)high--;
            nums[low] = nums[high];
            while(low < high && nums[low] <= flag)low ++;
            nums[high] = nums[low];
        }
        nums[low] = flag;
        return low;
    }
    public void fullss(int[] nums, int low, int high){
        Random rand = new Random();
        for(int i = 0; i < nums.length; i ++){
            int r = i + rand.nextInt(nums.length-i);
            int temp = nums[i];
            nums[i] = nums[r];
            nums[r] = temp;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值