|
题目描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
示例1
输入:[3,32,321]
返回值:"321323"
实现代码
class Solution:
#快排
def quick_sort(self,arr,left,right):
if left>right:return
i = left
j = right
temp = arr[left]
while i!=j:
while arr[j]+temp>=temp+arr[j] and i<j:
j -= 1
while arr[i]+temp<=temp+arr[i] and i<j:
i += 1
if i<j:
arr[i],arr[j] = arr[j],arr[i]
arr[left],arr[i] = arr[i],arr[left]
self.quick_sort(arr,left,i-1)
self.quick_sort(arr,i+1,right)
return arr
#希尔排序
def shell_sort(self,arr):
n = len(arr)
gap = n//2
while gap>0:
for i in range(gap,n):
tmp = arr[i]
j = i
while j>=gap and arr[j-gap]+tmp>tmp+arr[j-gap]:
arr[j] = arr[j-gap]
j -=gap
#gap的前半段
arr[j] = tmp
gap = gap//2
return arr
def PrintMinNumber(self, numbers):
# write code here
if not numbers: return ""
strs = [str(num) for num in numbers]
#self.quick_sort(strs, 0, len(strs)-1)
self.shell_sort(strs)
return ''.join(strs)
if len(self.stack)==0:
return None
return self.min_stack[-1]
|
|