admin管理员组

文章数量:1516870

【Leetcode】

问题描述

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