题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
分析:
code:
class Solution:
def theMax(self, str1, str2):
'''定义字符串比较函数'''
return str1 if str1+str2 > str2+str1 else str2
def PrintMinNumber(self, numbers):
"""使用冒泡进行排序(把最大的放最后)"""
#string1 = []
#for num in numbers:
#string = string.append(str(num))
string = [str(num) for num in numbers]
res = []
flag = True
count = len(string) - 1
while flag and count > 0:
flag = False
for i in range(len(string)-1):
if self.theMax(string[i], string[i+1]) == string[i]:
temp = string[i]
del string[i]
string.insert(i+1, temp)
flag = True
count -= 1
string = ''.join(string)
return string
'''
参考网页:https://www.cnblogs.com/lliuye/p/9159152.html
'''
![](https://img.haomeiwen.com/i18794076/d2347fe39646bf3a.png)
网友评论