179 Largest Number
179 Largest Number
Largest Number 
Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.
Since the result may be very large, so you need to return a string instead of an integer.
Example 1:
1
2
3
4
**Input:** nums = [10,2]
**Output:** "210"
Example 2:
1
2
3
4
**Input:** nums = [3,30,34,5,9]
**Output:** "9534330"
Constraints:
1
2
1 <= nums.length <= 100
0 <= nums[i] <= 109
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
from functools import cmp_to_key
def largestNumber(self, nums: List[int]) -> str:
def my_comparator(x,y):
return int(str(x) + str(y)) - int(str(y) + str(x))
nums.sort(reverse=True, key=cmp_to_key(my_comparator))
res = ''.join(str(x) for x in nums)
i = 0
while(i < len(res)):
if res[i] != '0':
return res[i:]
i += 1
return "0"
This post is licensed under CC BY 4.0 by the author.