-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddStrings.cpp
41 lines (41 loc) · 1.11 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
string addStrings(string num1, string num2) {
unordered_map<char, int> m = {
{'0', 0},{'1', 1},{'2', 2},{'3', 3},{'4', 4},{'5', 5},{'6', 6},{'7', 7},{'8', 8},{'9', 9},
};
unordered_map<int, string> rev_m = {
{0, "0"},{1, "1"},{2, "2"},{3, "3"},{4, "4"},{5, "5"},{6, "6"},{7, "7"},{8, "8"},{9, "9"},
};
string res="";
int carry = 0;
int i = num1.size()-1;
int j = num2.size()-1;
while(i>=0 && j>=0){
int x =carry + m[num1[i]] + m[num2[j]];
carry = x/10;
x = x%10;
res = rev_m[x] +res;
i--;
j--;
}
while(i>=0){
int x = carry + m[num1[i]];
carry = x/10;
x = x%10;
res = rev_m[x] + res;
i--;
}
while(j>=0){
int x = carry + m[num2[j]];
carry = x/10;
x = x%10;
res = rev_m[x] + res;
j--;
}
if(carry!=0){
res = rev_m[carry] + res;
}
return res;
}
};