python3 sort 排序 自定义函数 cmp 重写__lt__即可
179. 最大数
难度中等
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
[10,2]
210
示例 2:
[3,30,34,5,9]
9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class LargerNumKey( str ): def __lt__(x, y): return x + y < y + x class Solution( object ): def largestNumber( self , nums): """ :type nums: List[int] :rtype: str """ str_nums = sorted ([ str (n) for n in nums], key = LargerNumKey, reverse = True ) ans = "".join(str_nums) if ans = = "0" * len (ans): return "0" return ans |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2018-04-01 leetcode 661. Image Smoother
2018-04-01 python二维数组初始化
2018-04-01 leetcode 599. Minimum Index Sum of Two Lists
2018-04-01 Python中的sort() key含义
2018-04-01 leetcode 447. Number of Boomerangs
2018-04-01 leetcode 697. Degree of an Array
2018-04-01 leetcode 206. Reverse Linked List