Re: [閒聊] 每日leetcode
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
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 32 之 1548 篇):