当前位置:网站首页>[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
边栏推荐
- Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- How should programmers write logs
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- Promise optimized callback hell
- Pointer - function pointer
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- Pointer and string
- 大话云原生之负载均衡篇-小饭馆客流量变大了
- Market Research - current situation and future development trend of environmental friendly fireworks Market
猜你喜欢

Oracle cursor

开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...

New feature of go1.18: introduce new netip Network Library

建立自己的网站(22)

UE4 游戏架构 学习笔记

Source code analysis - lightweight asynchronous crawler framework Ruia

It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #

kubernetes 使用主机名将 pod 分配在指定节点上

20220702 how do programmers build knowledge systems?
![NC24325 [USACO 2012 Mar S]Flowerpot](/img/cf/86acbcb524b3af0999ce887c877781.png)
NC24325 [USACO 2012 Mar S]Flowerpot
随机推荐
php优化foreach中的sql查询
UE4 游戏架构 学习笔记
NC24325 [USACO 2012 Mar S]Flowerpot
杰理之、产线装配环节【篇】
php实现根据输入的年龄查询出生日期符合的数据
Socket套接字C/S端流程
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
杰理之如何测试按键的误触率【篇】
Mathematical modeling -- graph and network models and methods (I)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Using emqx cloud to realize one machine one secret verification of IOT devices
大话云原生之负载均衡篇-小饭馆客流量变大了
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
#include errors detected. Please update your includePath.
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
UE4 UI自适应屏幕
Pointer - function pointer
PHP wechat red packet grabbing algorithm
Methods of adding styles to native JS