Created
April 2, 2020 05:34
-
-
Save AntiKnot/65607a9b7d187023adb5a6fcdc3928b9 to your computer and use it in GitHub Desktop.
leetcode的题目
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
利用到python特性,覆盖list.sort的cmp比较方法调用的key