题解 | #寻找第K大#

寻找第K大

https://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param a int整型一维数组 
# @param n int整型 
# @param K int整型 
# @return int整型
#
class Solution:
    def findKth(self , a: List[int], n: int, K: int) -> int:
        # write code here
        # a倒序
        a.sort()
        a=a[::-1]
        # 初始化新数组b和累加器
        b=[]
        count=0
        # 累加要找的第n大数,每次分别把最大的数据加到数组b,并且在a中删除。
        while count<K:
            temp=max(a)
            b.append(temp)
            a.remove(temp)
            count=count+1
            print(count,K,temp,b)
        print(a,b)
        # 返回b中最小元素,即为要找的第n大的数
        return min(b)

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务