Re: [閒聊] 每日leetcode

作者: dont   2024-09-03 22:35:08
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
```
作者: sustainer123 (caster)   2024-09-03 22:36:00
大師
作者: DJYOMIYAHINA (通通打死)   2024-09-03 22:44:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com