zoukankan      html  css  js  c++  java
  • 数组_leetcode215

    #coding=utf-8
    # 解题思路:快速排序 20190302 找工作期间
    # 关键点 index = partion(xx) ,index表示的是在整个数组中的位置

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

    length = len(nums)

    if not nums or k <= 0 or k > length:
    return

    start = 0
    end = length-1
    self.fastSort(nums,start,end)

    return nums[length-k]

    def fastSort(self,alist,start,end):
    if end <= start:
    return

    base = alist[start]
    index1 , index2 = start,end

    while start < end :

    while start < end and alist[end] >= base:
    end -= 1
    alist[start] = alist[end]

    while start < end and alist[start] <= base:
    start += 1
    alist[end] = alist[start]

    alist[start] = base

    self.fastSort(alist,index1,start-1)
    self.fastSort(alist,start+1,index2)



    class Solution2(object):
    def findKthLargest(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: int
    """

    length = len(nums)

    if not nums or k <= 0 or k > length:
    return

    start = 0
    end = length-1
    target = length-k

    index = self.partition(nums,start,end)

    while index != target:
    if index > target:
    index = self.partition(nums,start,index-1)
    else:
    index = self.partition(nums,index+1,end)

    return nums[target]

    def partition(self,alist,start,end):

    if end <= start:
    return start

    base = alist[start]
    index1 , index2 = start , end

    while start < end :
    while start < end and alist[end] >= base :
    end -= 1
    alist[start] = alist[end]

    while start < end and alist[start] <= base:
    start += 1
    alist[end] = alist[start]

    alist[start] = base
    return start



    nums1 = [3,2,1,5,6,4]
    k1 = 2

    nums2 = [3,2,3,1,2,4,5,5,6]
    k2 = 4


    s = Solution2()

    print s.findKthLargest(nums2,k2)
  • 相关阅读:
    tp5 select回显
    toFixed
    用js来实现银行家算法
    js 日期证有效性验的通用方法
    js获取或判断任意数据类类型的通用方法(getDataType)和将NodeList转为数组(NodeListToArray)
    js实现jquery函数animate动画效果
    js原生实现 无缝滚动图片
    scrollTop实现图像循环滚动(实例1)
    commonCookie.js
    delphi XE3解析JSON数据
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10546878.html
Copyright © 2011-2022 走看看