[leetcode sort]179. Largest Number
Posted wilderness
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode sort]179. Largest Number相关的知识,希望对你有一定的参考价值。
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9]
, the largest formed number is 9534330
.
Note: The result may be very large, so you need to return a string instead of an integer.
对一组数字排序,使其组成的数字最大
思路:排序,不过重新定义排序规则,比如字符串x和字符串y,比较x+y和y+x的大小,如果x+y>y+x,则x>y
1 class Solution: 2 def largestNumber(self, nums): 3 nums = [str(n) for n in nums] 4 nums.sort(cmp=lambda y,x:cmp(x+y,y+x)) 5 return ‘‘.join(nums).lstrip(‘0‘) or ‘0‘
以上是关于[leetcode sort]179. Largest Number的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_num179_Largest Number
LeetCode 179. Largest Number (最大数)