当前位置:网站首页>【LeetCode】数组中第K大的元素
【LeetCode】数组中第K大的元素
2022-06-12 22:17:00 【LawsonAbs】
1.题目
2. 思想
手写快排,然后得到第k个最大元素
3. 代码
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
self.quickSort(nums,0,len(nums)-1) # 排序
# print(nums)
return nums[-k]
def quickSort(self,nums,left,right):
if left >= right: # 递归边界
return
mid = self.partition(nums,left,right)
self.quickSort(nums,left,mid-1) # 左半部分快排
self.quickSort(nums,mid+1,right) # 右半部分快排
# 将区间分成左右两部分(对每个部分进行快排)
def partition(self,nums,left,right):
pivot = nums[left]
pre_left = left
while(left < right):
while(left < right and nums[right] >= pivot):
right -= 1
nums[left] = nums[right]
while(left < right and nums[left] <= pivot):
left += 1
nums[right] = nums[left]
nums[left] = pivot
# 返回pivot的坐标
# print(nums)
return right
最后为了避免被特殊数据坑,我们可以在取pivot值的时候,稍微做一个变换。
mid = (left+right)//2
# 交换两个未知的值
nums[mid],nums[left] = nums[left],nums[mid]
pivot = nums[left]
边栏推荐
- The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
- JVM foundation > CMS garbage collector
- JVM foundation > GC generation: minorgc majorgc fullgc mixed GC
- [Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks
- JVM foundation > G1 garbage collector
- #yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
- JVM Basics - > What are the JVM parameters?
- Ansible playbook和Ansible Roles(三)
- MySQL介绍和安装(一)
- 2022-02-28 incluxdb high availability planning
猜你喜欢
【概率论与数理统计】期末复习抱佛脚:公式总结与简单例题(完结)
Ansible playbook和变量(二)
Ansible playbook和Ansible Roles(三)
PCB package download website recommendation and detailed usage
JVM Basics - > how to troubleshoot JVM problems in your project
SQL tuning guide notes 18:analyzing statistics using optimizer statistics Advisor
A puzzle about + =
JVM foundation > G1 garbage collector
SQL tuning guide notes 13:gathering optimizer statistics
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
随机推荐
SQL tuning guide notes 13:gathering optimizer statistics
[Jianzhi offer simple] Jianzhi offer 06 Print linked list from end to end
Use group_ Dplyr issues when using group_ by(multiple variables)
[sword finger offer] sword finger offer 58 - ii Rotate string left
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
Unity commonly used 3D mathematical calculation
Have you really learned the common ancestor problem recently?
logstash时间戳转换为unix 纳秒nano second time
2023届校园招聘正式开启!OceanBase 想和你在这个春天约一场面试
Oracle livelabs experiment: introduction to Oracle Spatial
(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office
How to perform disaster recovery and recovery for kubernetes cluster? (22)
SQL tuning guide notes 11:histograms
动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)
Role of volatile keyword
Open source background management system suitable for outsourcing projects
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
[Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks
在同花顺开户证券安全吗,买股票怎么网上开户
February 27th