【Leetcode】| 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.

  • 例如: 列表[3, 30, 34, 5, 9],组成的最大数为 9534330.

注意事项

Note: The result may be very large, so you need to return a string instead of an integer.

  • 注意:结果可能非常大,因此需要返回一个字符串而不是整形数。

代码

def largestNum(nums):
    nums = sorted([str(i) for i in nums], cmp=lambda x, y: cmp(y + x, x + y))
    return "".join(nums).lstrip() or '0'


print  largestNum([3, 30, 34, 5, 9])

你可能感兴趣的:(leetcode,python,算法,largest,num,Leetcode算法学习)