当前位置:网站首页>【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]
边栏推荐
- Database daily question --- day 10: combine two tables
- Things about the kotlin collaboration process - pipeline channel
- A puzzle about + =
- 经济学人聚焦WTO MC12:数字经济或成重要议题
- [sword finger offer simple] sword finger offer 24 Reverse linked list
- Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
- Economist focuses on WTO MC12: digital economy may become an important issue
- LNMP platform docking redis service
- Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!
- MySQL introduction and installation (I)
猜你喜欢

Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!

How to specify your webpage's language so Google Chrome doesn't offer to translate it

Prefix sum and difference

Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?

SQL tuning guide notes 14:managing extended statistics

Ansible playbook and ansible roles (III)

"Oracle database parallel execution" technical white paper reading notes

Have you really learned the common ancestor problem recently?

Qt Quick 3D学习:鼠标拾取物体

Oracle livelabs experiment: introduction to Oracle Spatial
随机推荐
JVM foundation - > talk about class loader two parent delegation model
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)
USB机械键盘改蓝牙键盘
Configuring Dingding notification of SQL audit platform archery
JVM foundation > CMS garbage collector
Ansible playbook and variable (II)
What is the difference between volatile variables and atomic variables?
How to prevent phishing emails? S/mime certificate to help!
Qt Quick 3D学习:鼠标拾取物体
USB mechanical keyboard changed to Bluetooth Keyboard
Dolphin-2.0.3 cluster deployment document
SQL tuning guide notes 9:joins
flutter系列之:flutter中常用的GridView layout详解
脱颖而出!OceanBase 入选 2021“科创中国”开源创新榜单
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
Ansible Roles-项目案例(四)
Build a highly available database
在同花顺开户证券安全吗,买股票怎么网上开户
MySQL architecture and basic management (II)
2021 rust survey results released: 9354 questionnaires collected