We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4ec32d0 commit d0d4642Copy full SHA for d0d4642
Python/largest-number.py
@@ -12,26 +12,10 @@ class Solution:
12
# @param num, a list of integers
13
# @return a string
14
def largestNumber(self, num):
15
- largest = ""
16
- nonzero = False
17
- for n in sorted(num, cmp=self.cmp_items):
18
- if n != 0:
19
- nonzero = True
20
- if nonzero:
21
- largest += "{}".format(n)
22
-
23
- if largest == "":
24
- largest = "0"
25
26
- return largest
27
28
- def cmp_items(self, a, b):
29
- if a == b:
30
- return 0
31
- elif "{}{}".format(a, b) > "{}{}".format(b, a):
32
- return -1
33
- else:
34
- return 1
+ num = [str(x) for x in num]
+ num.sort(cmp=lambda x, y: cmp(y + x, x + y))
+ ret = ''.join(num)
+ return ret.lstrip('0') or '0'
35
36
if __name__ == "__main__":
37
num = [3, 30, 34, 5, 9]
0 commit comments