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

LeetCode Largest Number

时间:2015-01-27 21:51:15      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

Problem: 

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.

 

Solution:

 1 class Solution:
 2     # @param num, a list of integers
 3     # @return a string
 4     def largestNumber(self, num):
 5         def cmpFunc(x, y):
 6             xStr = str(x)
 7             yStr = str(y)
 8             
 9             yStr += xStr
10             xStr += yStr
11             return cmp(xStr, yStr)
12             
13         num = sorted(num, cmp=cmpFunc,reverse=True)
14         res = reduce(lambda acum, x: acum + str(x), num,‘‘)
15         if res[0] == 0:
16             return 0
17         else:
18             return res

 

LeetCode Largest Number

标签:

原文地址:http://www.cnblogs.com/liew/p/4254026.html

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