-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddstrings.cpp
30 lines (30 loc) · 873 Bytes
/
addstrings.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
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.length()-1, j = num2.length()-1;
int dig, carry = 0;
string ans = "";
while (i > -1 && j > -1) {
dig = (num1[i]-'0' + num2[j]-'0' + carry)%10;
carry = (num1[i]-'0' + num2[j]-'0' + carry)/10;
ans = to_string(dig) + ans;
i--;
j--;
}
while (i > -1) {
dig = (num1[i]-'0' + carry)%10;
carry = (num1[i]-'0' + carry)/10;
ans = to_string(dig) + ans;
i--;
}
while (j > -1) {
dig = (num2[j]-'0' + carry)%10;
carry = (num2[j]-'0' + carry)/10;
ans = to_string(dig) + ans;
j--;
}
if (carry)
ans = to_string(carry) + ans;
return ans;
}
};