python | #最小的K个数#
最小的K个数
http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf
# -*- coding:utf-8 -*- class Solution: def GetLeastNumbers_Solution(self, tinput, k): # write code here if len(tinput) == 0 or k == 0: return [] if len(tinput) == k: return tinput left = 0 right = len(tinput)-1 l = tinput left = self.quick_sort(l, left, right) while left != k-1: if left < k-1: left = self.quick_sort(l, left+1, right) if left > k-1: left = self.quick_sort(l, 0,left -1 ) return l[:k] # tinput.sort() # return tinput[:k] def quick_sort(self,l,left,right): low = left point = l[left] high = right while low < high: while low<high and point <= l[high]: high -= 1 while low<high and point >= l[low]: low += 1 self.swap(l, low,high) self.swap(l, left, low) return low def swap(self,l,i,j): temp = l[i] l[i]= l[j] l[j] = temp