lc215

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

215.数组中的第K个最大元素

1. 题目描述

给定整数数组 nums 和整数 k ,请返回数组中第 **k** 个最大的元素。

请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

 

示例 1:


输入: [3,2,1,5,6,4] 和 k = 2
输出: 5

示例 2:


输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4

 

提示:

  • 1 <= k <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4

标签 数组 分治 快速选择 排序 堆(优先队列)

2. 解题

1.使用内置函数sort排序,返回排序后的倒数第k个数
2.使用快速排序,比较每次快排后的low指针和len(nums)-k的位置关系,若刚好相等,则返回该值,若不相等,在左右区间上继续采用快排。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL