python在Leetcode最大数量中使用cmp_to_key

时间:2019-03-05 18:49:12

标签: python-3.x

我正在尝试使用Python 3解决leetcode中的这个问题: https://leetcode.com/problems/largest-number/

给出的解决方案使用另一个类,对我来说似乎并不优雅。

所以我尝试使用cmp_to_key来解决它。

def largestNumber(nums: 'List[int]') -> str:
    num = ''.join(sorted(list(map(str, nums)), key = cmp_to_key(lambda x, y: x + y > y + x)))
    return '0' if num[0] == '0' else num

但是我知道了

  

'3303459'

无论我在lambda函数中使用哪个运算符。我同时尝试了<>,但最终得到了相同的错误结果。并且在reverse中使用sorted选项也不起作用。

如何使用cmp_to_key解决此问题?

0 个答案:

没有答案