Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LC215. 数组中的第K个最大元素 #18

Open
HealUP opened this issue May 11, 2023 · 0 comments
Open

LC215. 数组中的第K个最大元素 #18

HealUP opened this issue May 11, 2023 · 0 comments
Labels
算法 记录刷题记录,代码随想录

Comments

@HealUP
Copy link
Owner

HealUP commented May 11, 2023

[215. 数组中的第K个最大元素](https://leetcode.cn/problems/kth-largest-element-in-an-array/)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        // 快速排序 小的放到右边,大的放到左边即可
        quickSort(nums, 0, nums.length - 1);
        return nums[k - 1];

    }

    public int[] quickSort(int[] nums, int left, int right) {
        if (left >= right) {
            return null;
        }
        int pivot = partion(nums, left, right);
        quickSort(nums, left, pivot - 1);
        quickSort(nums, pivot + 1,right);
        return nums;
    }

    public int partion(int[] nums, int left, int right) {
        int pivot = nums[left];

        while (left < right) {
            while (left < right && nums[right] <= pivot) {
                right--;
            }
            nums[left] = nums[right];
            while (left < right && nums[left] >= pivot) {
                left++;
            }
            nums[right] = nums[left];
        }

        nums[left] = pivot;
        return left;
    }
}
@HealUP HealUP added 算法 记录刷题记录,代码随想录 排序 labels May 11, 2023
@HealUP HealUP changed the title 215. 数组中的第K个最大元素 LC215. 数组中的第K个最大元素 May 11, 2023
@HealUP HealUP removed the 排序 label Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
算法 记录刷题记录,代码随想录
Projects
None yet
Development

No branches or pull requests

1 participant