Re: [閒聊] 每日leetcode

作者: DJYOMIYAHINA (通通打死)   2024-07-24 12:16:58
原來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))
作者: DJYOMIYAHINA (通通打死)   2024-07-24 12:17:00
然後好像可以直接用key 不用cmp_to_key
作者: rainkaras (rainkaras)   2024-07-24 12:19:00
大師
作者: sustainer123 (caster)   2024-07-24 12:20:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com