1. 题目来源
2. 题目解析
计算数位和就行了,第一次从数字转到数位和的时候别转成 10 进制了,直接爆没了…以为数据还是一如既往的弱,没想到是 100 位的字符串…
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
代码:
我的代码。
思路差不多,代码都是大同小异。
class Solution {
public:
int getLucky(string s, int k) {
int res = 0;
for (auto c : s) {
int t = c - 'a' + 1;
while (t) {
res += t % 10;
t /= 10;
}
}
for (int i = 1; i < k; i ++ ) {
int t = res, cnt = 0;
while (t) {
cnt += t % 10;
t /= 10;
}
res = cnt;
}
return res;
}
};