Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/09/03 22:35), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串802/1548 (看更多)
1945. Sum of Digits of String After Convert ## 思路 第1輪把字元都轉成數字再加總digit 第2~k輪就digit加總 ## Code ```python class Solution: def getLucky(self, s: str, k: int) -> int: res = 0 for ch in s: pos = ord(ch) - ord('a') + 1 res += pos // 10 + pos % 10 for _ in range(1, k): curr = 0 while res: res, digit = divmod(res, 10) curr += digit res = curr return res ``` -- http://i.imgur.com/OLvBn3b.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.146 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725374111.A.0F5.html

09/03 22:36, 1年前 , 1F
大師
09/03 22:36, 1F

09/03 22:44, 1年前 , 2F
大師
09/03 22:44, 2F
文章代碼(AID): #1crnwV3r (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1crnwV3r (Marginalman)