Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/07/24 12:17), 編輯推噓2(201)
留言3則, 3人參與, 1年前最新討論串558/1549 (看更多)
原來python的compare function要回傳數值不是bool== 搞了我好久 def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: m = {} for i in range(10): m[str(i)] = str(mapping[i]) def cmp(n1, n2): new_n1 = "".join([m[c] for c in str(n1)]) new_n2 = "".join([m[c] for c in str(n2)]) return int(new_n1) - int(new_n2) return sorted(nums, key=cmp_to_key(cmp)) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1721794620.A.944.html

07/24 12:17, 1年前 , 1F
然後好像可以直接用key 不用cmp_to_key
07/24 12:17, 1F

07/24 12:19, 1年前 , 2F
大師
07/24 12:19, 2F

07/24 12:20, 1年前 , 3F
大師
07/24 12:20, 3F
文章代碼(AID): #1ce80yb4 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ce80yb4 (Marginalman)