当前位置:网站首页>[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
边栏推荐
- Build your own website (22)
- Commodity information management system (C language document version)
- Utilisation de simpletk - 4. Question étrange
- Micro service gateway selection, please accept my knees!
- #include errors detected. Please update your includePath.
- It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
- Task and privilege level protection
- App page sharing password rails implementation
- 存储单位换算
- 开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
猜你喜欢
随机推荐
牛客网:龙与地下城游戏
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
Simpleitk use - 3 Common operations
PMP项目整合管理
原生js添加样式的方法
Try to get property'num for PHP database data reading_ rows' of non-object?
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
Market Research - current market situation and future development trend of handheld wound imaging equipment
[shutter] shutter application theme (themedata | dynamic modification theme)
php实现根据输入的年龄查询出生日期符合的数据
Learn computer knowledge from scratch
Pointer array parameter passing, pointer parameter passing
UE4 game architecture learning notes
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Market Research - current market situation and future development trend of aircraft audio control panel system
[QT] QT multithreading development - reentrancy and thread safety
Oracle-游标
Technological Entrepreneurship: failure is not success, but reflection is
Dynamic memory allocation (malloc calloc realloc free)
phpcms实现订单直接支付宝支付功能