Re: [閒聊] 每日leetcode

看板Marginalman作者 (みけねこ的鼻屎)時間1年前 (2024/03/11 09:41), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串32/1548 (看更多)
https://leetcode.com/problems/custom-sort-string/description 791. Custom Sort String 給你一個字串s和一個表示字元排序優先的字串order,排序字串s,如果字元不在 order 的話順序隨意。 思路: 1.創建一個基於order索引的comparator丟給排序函數 py code ---------------------------------------------- class Solution: def customSortString(self, order: str, s: str) -> str: dic = collections.defaultdict(lambda: ord(c) + 26) for i, c in enumerate(order): dic[c] = i return ''.join(sorted(s, key=lambda x: dic[x])) ---------------------------------------------- -- https://i.imgur.com/PIoxddO.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.139.24.59 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1710121301.A.D0D.html

03/11 09:46, 1年前 , 1F
大師
03/11 09:46, 1F
文章代碼(AID): #1bxc5LqD (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bxc5LqD (Marginalman)