Sort a list to form the largest possible number

前端 未结 8 1032
无人及你
无人及你 2020-11-27 20:29

I am trying to write a function that given a list of non negative integers, arranges them such that they form the largest possible number.

For example, given

8条回答
  •  独厮守ぢ
    2020-11-27 20:48

    I would love to understand from all the python experts here what is wrong with my one-liner solution. Leet code website keeps rejecting with failed tcs which works just fine on my local env.

    from itertools import permutations as pm
    
    def max_number(lst):
    
        if all(v == 0 for v in nums):
            return "0"
    
        lst1 = [str(item) for item in lst]
        return max([int(''.join(list(perm))) for perm in pm(lst, len(lst1))])
    

提交回复
热议问题