1 """
2 Given a list of non negative integers, arrange them such that they form the largest number.
3 Example 1:
4 Input: [10,2]
5 Output: "210"
6 Example 2:
7 Input: [3,30,34,5,9]
8 Output: "9534330"
9 """
10 """
11 用冒泡排序的思想,两两结合进行比较,如果小则交换位置
12 注意需要转为str结合再转为int比较
13 """
14 class Solution:
15 def largestNumber(self, nums):
16 res = ''
17 for i in range(len(nums)):
18 for j in range(i+1, len(nums)):
19 if int(str(nums[i])+str(nums[j])) < int(str(nums[j])+str(nums[i])):#!!!
20 nums[i], nums[j] = nums[j], nums[i]
21 if nums[0] == 0: ##bug Input: [0, 0] Output: "00" Expected: "0"
22 return '0'
23 for i in range(len(nums)):
24 res += str(nums[i])
25 return res