用int会越界的,所以先转成str
数字拼接满足结合律,所以只需要保证每两个数拼版接后更小就行了,即权ij?ji。因此,可以使用冒泡排序,但是比较的条件变成'i'+'j' > 'j' + 'i',如果满足就交换。
输出用print(''.join(list))即可,python打印字符串的时候不会带''的。
代码是:
# -*- coding:utf-8 -*-
class Solution:
def PrintMinNumber(self,numbers):
if not numbers:
return ''
n = len(numbers)
for i in range(n):
numbers[i] = str(numbers[i])
print(numbers)
for i in range(n):
for j in range(i+1,n):
if numbers[i] + numbers[j] > numbers[j] + numbers[i]:
numbers[i],numbers[j] = numbers[j],numbers[i]
return ''.join(numbers)