码迷,mamicode.com
首页 > 编程语言 > 详细

Leetcode 215. 数组中的第K个最大元素 By Python

时间:2018-10-14 19:04:18      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:def   code   思路   class   find   solution   pre   type   sorted   

在未排序的数组中找到第 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

思路

一个sorted再直接返回第K个最大元素就好了

代码

class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        return sorted(nums)[-k]

Leetcode 215. 数组中的第K个最大元素 By Python

标签:def   code   思路   class   find   solution   pre   type   sorted   

原文地址:https://www.cnblogs.com/MartinLwx/p/9787161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!