Skip to content

Commit 6c0fe15

Browse files
authored
Create 1945. Sum of Digits of String After Convert (#574)
2 parents f2d6f3a + b416d1f commit 6c0fe15

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int transform(int num){
4+
5+
int sum = 0;
6+
7+
while(num>0){
8+
9+
int digit = num % 10;
10+
sum += digit;
11+
num /= 10;
12+
13+
}
14+
15+
return sum;
16+
17+
}
18+
int getLucky(string s, int k) {
19+
20+
string str;
21+
22+
for (auto& c : s) {
23+
str += to_string((c - 'a') + 1);
24+
}
25+
26+
int sum = 0;
27+
for (auto& digits : str) {
28+
sum += digits - '0';
29+
}
30+
31+
for(int i=1;i<k;i++){
32+
sum = transform(sum);
33+
}
34+
35+
return sum;
36+
}
37+
};

0 commit comments

Comments
 (0)