当前位置:网站首页>[LeetCode] 数组中的第K个最大元素【215】
[LeetCode] 数组中的第K个最大元素【215】
2022-07-02 22:06:00 【山茶花开时。】
问题: 给定整数数组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
python3解题
# 解法1
def findKthLargest(nums, k):
nums.sort(reverse=True)
return nums[k-1]
findKthLargest([3,2,1,5,6,4],2) # 5
findKthLargest([3,2,3,1,2,4,5,5,6],4) # 4
涉及知识点:
[Python] sort()方法 VS sorted()函数https://blog.csdn.net/Hudas/article/details/122884550
边栏推荐
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- Promise optimized callback hell
- Introduction to database system Chapter 1 short answer questions - how was the final exam?
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- [shutter] shutter resource file use (import resource pictures | use image resources)
- Unity3d learning notes 4 - create mesh advanced interface
- Mathematical modeling -- graph and network models and methods (I)
- Lightgbm principle and its application in astronomical data
- 杰理之直接触摸样机的顶针反应不正常【篇】
- Market Research - current market situation and future development trend of aircraft front wheel steering system
猜你喜欢
电商系统微服务架构
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
UE4 game architecture learning notes
Simpleitk use - 3 Common operations
[shutter] shutter resource file use (import resource pictures | use image resources)
Share how to make professional hand drawn electronic maps
图形视图框架
PMP项目整合管理
540. Single element in ordered array
随机推荐
杰理之样机无触摸,拆机之后重新安装变正常【篇】
百度智能云-创建人脸识别应用
Micro service gateway selection, please accept my knees!
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
【外刊】睡眠与减肥
Promise optimized callback hell
JS获取display为none的隐藏元素的宽度和高度的解决方案
Pointer array parameter passing, pointer parameter passing
Unity3d learning notes 4 - create mesh advanced interface
ArrayList analysis 2: pits in ITR, listiterator, and sublist
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
SimpleITK使用——4. 奇怪的問題
PHP optimizes SQL queries in foreach
App page sharing password rails implementation
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
杰理之、产线装配环节【篇】
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
20220702 how do programmers build knowledge systems?