-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec4_AddString.cpp
51 lines (44 loc) Β· 1.04 KB
/
lec4_AddString.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
42
43
44
45
46
47
48
49
50
51
https://leetcode.com/problems/add-strings/
class Solution {
string add(string num1, string num2 )
{
int index1 = num1.size() - 1;
int index2 = num2.size() - 1;
string ans;
int carry = 0 , sum = 0 ;
while(index2>=0)
{
sum = (num1[index1] - '0') + (num2[index2] - '0') + carry ;
carry = sum/10 ;
char c = sum%10 + '0';
ans+=c;
index2-- , index1--;
}
//index left
while(index1>=0)
{
sum = num1[index1] - '0' + carry;
carry = sum/10;
char c = '0' + sum%10;
ans+=c;
index1--;
}
if(carry)
{
ans+='1';
}
reverse(ans.begin() , ans.end());
return ans;
}
public:
string addStrings(string num1, string num2) {
if(num1.size()<num2.size())
{
return add(num2 , num1);
}
else
{
return add(num1 , num2);
}
}
};