-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathadd_strings.cpp
More file actions
26 lines (25 loc) · 812 Bytes
/
add_strings.cpp
File metadata and controls
26 lines (25 loc) · 812 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
class Solution { // 4ms
public:
string addStrings(string num1, string num2) {
int c=0;
string ans="";
cout<<num1<<" , "<<num2<<endl;
while(!num1.empty() || !num2.empty() || c!=0){
int x = (!num1.empty()) ? num1[num1.size()-1] - '0' : 0;
int y = (!num2.empty()) ? num2[num2.size()-1] - '0' : 0;
int z = x+y+c;
int v = z % 10;
ans.insert(0, to_string(v));
c = z/10;
// cout<<x<<" , "<<y<<" , "<<c<<" , "<<z << " : "<<ans<<endl;
// cout<<num1<<" , "<<num2<<" , "<<c<<endl;
if(!num1.empty()){
num1.pop_back();
}
if(!num2.empty()){
num2.pop_back();
}
}
return ans;
}
};