Python, comparing sorted function with recursion, sorted win!


  • 0
    R

    0_1473922568441_pythoncompare.png 0_1473922588748_pythoncode.png
    class Solution(object):
    def findKthLargest(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: int
    flag = nums[k-1]
    bigger = []
    smaller = []
    for index, num in enumerate(nums):
    if index == k-1:
    continue
    if num < flag:
    smaller.append(num)
    else:
    bigger.append(num):
    if len(bigger) == k-1:
    return flag
    elif len(bigger) >= k:
    return self.findKthLargest(bigger, k)
    else
    return self.findKthLargest(smaller, k-len(bigger)-1)
    """
    return sorted(nums)[-k]

    • list item

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.