模拟即可。
class Solution {
public:
int getLucky(string s, int k) {
int n=0;
string t;
for(auto c:s) t+=to_string(c-'a'+1);
for(auto c:t) n+=c-'0';
while(k-->1){
int res=0;
while(n){
res+=n%10;
n/=10;
}
n=res;
}
return n;
}
};
时间复杂度:O(km),m为s的长度。
空间复杂度:O(m)。
本文介绍了一个简单的算法,用于计算字符串的幸运数值。通过将每个字符转换为其在字母表中的位置,然后求和,并重复对该和进行同样的操作直到达到指定次数,最终得到一个整数结果。
855

被折叠的 条评论
为什么被折叠?



