Re: [閒聊] 每日leetcode
看板Marginalman作者JerryChungYC (JerryChung)時間1年前 (2024/09/03 09:09)推噓1(1推 0噓 0→)留言1則, 1人參與討論串799/1549 (看更多)
※ 引述《sustainer123 (caster )》之銘言:
: ※ 引述《enmeitiryous (enmeitiryous)》之銘言:
: : 今天是easy
: : 題目:
: : 1945 sum of digits of string after convert
: : 給你一個英文字串將他依照a=1,b=2...的方式轉換成一串數字後,求這串數字在經過
: : k次把每一位數總和後的整數為何?
: : 思路:
: : 照做,使用to_string和-'0'來進行字串和整數的互換
: : int getLucky(string s, int k) {
: : string pre_ans="";
: : for(int i=0;i<s.size();++i){
: : pre_ans+=to_string(s[i]-96);
: : }
: : int ans=0;
: : while(k!=0){
: : ans=0;
: : k--;
: : for(int j=0;j<pre_ans.size();++j){
: : ans+=(pre_ans[j]-'0');
: : }
: : pre_ans=to_string(ans);
: : }
: : return ans;
: : }
: 思路:
: 照著敘述做
: Python Code:
: class Solution:
: def getLucky(self, s: str, k: int) -> int:
: n = ""
: for c in s:
: n += str(ord(c) - ord("a") + 1)
: while k > 0:
: a = 0
: n = str(n)
: for i in n:
: a += int(i)
: n = a
: k -= 1
: return n
絲路:
照著做
Python Code:
class Solution:
def getLucky(self, s: str, k: int) -> int:
res = ''.join(str(ord(_) - 96) for _ in s)
while k:
res = sum(int(_) for _ in str(res))
k -= 1
return res
原本想從9月開始重寫 結果第一天就被打敗 我這輩子就這樣了
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.52.67 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725325756.A.335.html
推
09/03 09:09,
1年前
, 1F
09/03 09:09, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 799 之 1549 篇):