-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-1945
More file actions
41 lines (31 loc) · 822 Bytes
/
LeetCode-1945
File metadata and controls
41 lines (31 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Problem : https://leetcode.com/problems/sum-of-digits-of-string-after-convert
// Solution :
class Solution {
public:
int digitalSum(int n){
int sum = 0;
while(n){
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
int getLucky(string s, int k) {
string modifiedString = "";
for(int i = 0; i < s.size(); i++){
int charValue = (s[i] - 'a' + 1);
modifiedString += to_string(charValue);
}
//int num = stoi(modifiedString);
//int sum = digitalSum(num);
int sum = 0;
for(int it : modifiedString){
sum += it - '0';
}
for(int i = 1; i < k; i++){
sum = digitalSum(sum);
}
return sum;
}
};