首页 > 代码库 > LeetCode Largest Number
LeetCode Largest Number
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 += xStr10 xStr += yStr11 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。