Skip to content

Instantly share code, notes, and snippets.

@AntiKnot
Created April 2, 2020 05:34
Show Gist options
  • Select an option

  • Save AntiKnot/65607a9b7d187023adb5a6fcdc3928b9 to your computer and use it in GitHub Desktop.

Select an option

Save AntiKnot/65607a9b7d187023adb5a6fcdc3928b9 to your computer and use it in GitHub Desktop.
leetcode的题目
import functools
from typing import List
class Solution(object):
def largest_nums(self, nums: List[int]) -> str:
snums = [str(item) for item in nums]
snums.sort(key=functools.cmp_to_key(self.cmp), reverse=True)
_res = ""
for i in snums:
_res += i
return _res
@staticmethod
def cmp(s1, s2):
s12 = s1 + s2
s21 = s2 + s1
if s12 < s21:
return -1
elif s12 > s21:
return 1
return 0
if __name__ == '__main__':
solution = Solution()
nums = [3, 30, 34, 5, 9]
res = solution.largest_nums(nums)
print(res)
@AntiKnot
Copy link
Copy Markdown
Author

AntiKnot commented Apr 2, 2020

利用到python特性,覆盖list.sort的cmp比较方法调用的key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment