[leetcode sort]179. 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.

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‘
时间: 2024-10-13 03:45:29

[leetcode sort]179. Largest Number的相关文章

【LeetCode】179. Largest Number

Description: For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Analysis: The problem can be solved by sorting. It's reallllllllllllllllllllllllllllllllllllllllllly a tallent and brilliant idea to give a compare function like

leetCode 179. Largest Number 字符串排序 | Medium

179. 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. Note: The result may be very large, so you need to return a st

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link 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 ver

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

184. Largest Number/179. Largest Number 本题难度: Medium Topic: Greedy Description Largest Number 中文English Given a list of non negative integers, arrange them such that they form the largest number. Example Given [1, 20, 23, 4, 8], the largest formed nu

No.179 Largest Number

No.179 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. Note: The result may be very large, so you need to return a

[LeetCode] 179. 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. Note: The result may be very large, so you need to return a string instead of an i

leetcode[179]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. Note: The result may be very large, so you need to return a string instead of an i

Java for LeetCode 179 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. Note: The result may be very large, so you need to return a string instead of an i

[LeetCode] 179. Largest Number Java

题目: 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