leetcode179 Largest Number
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步