力扣labuladong——一刷day39

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


前言


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

一、力扣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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在VS Code上刷力扣LeetCode)题目,首先需要进行以下几步操作: 1. 安装VS Code插件:在VS Code中搜索并安装LeetCode插件。这个插件可以提供LeetCode题目的在线编写和提交功能,以及自动测试和调试代码的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【史上最强代码编辑器VS Code】之VS Code刷力扣LeetCode)题目](https://blog.csdn.net/weixin_44553006/article/details/105183522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [在 vscode 上刷力扣 Leetcode 可以这样来](https://blog.csdn.net/u012190388/article/details/121277555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [leetcode答案-algo-study:从零开始刷力扣(LeetCode),JavaScript语言,算法](https://download.csdn.net/download/weixin_38680764/19920528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乱世在摸鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值