-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
35 lines (34 loc) · 908 Bytes
/
main.cpp
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
class Solution {
public:
string digitSum(string s, int k) {
while (s.size() > k) {
int n = s.size();
string new_s;
for (int l = 0; l < s.size(); l += k) {
int r = min(l + k, int(s.size()));
int v = 0;
for (char c : s.substr(l, r - l)) v += c - '0';
new_s += to_string(v);
}
s = new_s;
}
return s;
}
};
class Solution {
public:
string digitSum(string s, int k) {
while (s.size() > k) {
string new_s;
for (int i = 0; i < s.size(); i += k) {
int v = 0;
for (char c : s.substr(i, min(k, (int)s.size() - i))) {
v += c - '0';
}
new_s += to_string(v);
}
s = move(new_s);
}
return s;
}
};