码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode sort]179. Largest Number

时间:2017-03-08 22:40:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:字符串   ges   log   integer   最大   .so   color   ati   join   

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

标签:字符串   ges   log   integer   最大   .so   color   ati   join   

原文地址:http://www.cnblogs.com/fcyworld/p/6523078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!