Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/11/04 21:11), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1073/1548 (看更多)
3163. String Compression III ## 思路 照題目作 記錄前一個字母跟累計個數 ## Code ```python class Solution: def compressedString(self, word: str) -> str: res = [] cnt = 0 prev = word[0] for ch in word: if ch != prev: res.append(f'{cnt}{prev}') cnt, prev = 1, ch elif cnt == 9: res.append(f'{cnt}{ch}') cnt = 1 else: cnt += 1 res.append(f'{cnt}{ch}') return ''.join(res) ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1730725870.A.477.html
文章代碼(AID): #1dACVkHt (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dACVkHt (Marginalman)