Skip to content
LC-0179 Medium LeetCode

179. Largest Number

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 41% Topics: Array, String, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(1)

class Solution(object):
    # @param num, a list of integers
    # @return a string
    def largestNumber(self, num):
        num = [str(x) for x in num]
        num.sort(cmp=lambda x, y: cmp(y + x, x + y))
        largest = ''.join(num)
        return largest.lstrip('0') or '0'

Solution from kamyu104/LeetCode-Solutions · MIT