[剑指Offer]最小的K个数[Python]

题目要求:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

解题思路:最简单的冒泡排序,当然不是最有答案,复杂度太大O(nk)

# -*- coding:utf-8 -*-
class Solution:
    def GetLeastNumbers_Solution(self, tinput, k):
        res = []
        if tinput and k<=len(tinput):
            for i in range(k):
                jjend = len(tinput)-1
                for jj in range(jjend):
                    if tinput[jj] < tinput[jj+1]:
                        tinput[jj],tinput[jj+1] = tinput[jj+1],tinput[jj]
                res.append(tinput.pop())
            return res
        else:

            return []

看到牛友把好多种排序方法都实现了,膜拜一下,先复制下来,以后看一下:

链接:https://www.nowcoder.com/questionTerminal/6a296eb82cf844ca8539b57c23e6e9bf
来源:牛客网

方法一:蒂姆排序
1
2
3
4
5
6
7
8
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         if tinput = = [] or k > len (tinput):
             return []
         tinput.sort()
         return tinput[: k]
方法二:快速排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def quick_sort(lst):
             if not lst:
                 return []
             pivot = lst[ 0 ]
             left = quick_sort([x for x in lst[ 1 : ] if x < pivot])
             right = quick_sort([x for x in lst[ 1 : ] if x > = pivot])
             return left + [pivot] + right
 
         if tinput = = [] or k > len (tinput):
             return []
         tinput = quick_sort(tinput)
         return tinput[: k]
方法三:归并排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def merge_sort(lst):
             if len (lst) < = 1 :
                 return lst
             mid = len (lst) / / 2
             left = merge_sort(lst[: mid])
             right = merge_sort(lst[mid:])
             return merge(left, right)
         def merge(left, right):
             l, r, res = 0 , 0 , []
             while l < len (left) and r < len (right):
                 if left[l] < = right[r]:
                     res.append(left[l])
                     l + = 1
                 else :
                     res.append(right[r])
                     r + = 1
             res + = left[l:]
             res + = right[r:]
             return res
         if tinput = = [] or k > len (tinput):
             return []
         tinput = merge_sort(tinput)
         return tinput[: k]
方法四:堆排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def siftup(lst, temp, begin, end):
             if lst = = []:
                 return []
             i, j = begin, begin * 2 + 1
             while j < end:
                 if j + 1 < end and lst[j + 1 ] > lst[j]:
                     j + = 1
                 elif temp > lst[j]:
                     break
                 else :
                     lst[i] = lst[j]
                     i, j = j, 2 * j + 1
             lst[i] = temp
 
         def heap_sort(lst):
             if lst = = []:
                 return []
             end = len (lst)
             for i in range ((end / / 2 ) - 1 , - 1 , - 1 ):
                 siftup(lst, lst[i], i, end)
             for i in range (end - 1 , 0 , - 1 ):
                 temp = lst[i]
                 lst[i] = lst[ 0 ]
                 siftup(lst, temp, 0 , i)
             return lst
 
         if tinput = = [] or k > len (tinput):
             return []
         tinput = heap_sort(tinput)
         return tinput[: k]
方法五:冒泡排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def bubble_sort(lst):
             if lst = = []:
                 return []
             for i in range ( len (lst)):
                 for j in range ( 1 , len (lst) - i):
                     if lst[j - 1 ] > lst[j]:
                         lst[j - 1 ], lst[j] = lst[j], lst[j - 1 ]
             return lst
 
         if tinput = = [] or k > len (tinput):
             return []
         tinput = bubble_sort(tinput)
         return tinput[: k]
方法六:直接选择排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def select_sort(lst):
             if lst = = []:
                 return []
             for i in range ( len (lst) - 1 ):
                 smallest = i
                 for j in range (i, len (lst)):
                     if lst[j] < lst[smallest]:
                         smallest = j
                 lst[i], lst[smallest] = lst[smallest], lst[i]
 
             return lst
 
         if tinput = = [] or k > len (tinput):
             return []
         tinput = select_sort(tinput)
         return tinput[: k]
方法七:插入排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8 -*-
class Solution:
     def GetLeastNumbers_Solution( self , tinput, k):
         # write code here
         def Insert_sort(lst):
             if lst = = []:
                 return []
             for i in range ( 1 , len (lst)):
                 temp = lst[i]
                 j = i
                 while j > 0 and temp < lst[j - 1 ]:
                     lst[j] = lst[j - 1 ]
                     j - = 1
                 lst[j] = temp
             return lst
 
         if tinput = = [] or k > len (tinput):
             return []
         tinput = Insert_sort(tinput)
         return tinput[: k]

猜你喜欢

转载自blog.csdn.net/jillian_sea/article/details/80357023