Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.
class Solution {
public:
string addStrings(string num1, string num2) {
string res = "";
int n1 = num1.length()-1;
int n2 = num2.length()-1;
int carry = 0;
while(n1>=0||n2>=0||carry)
{
long sum = 0;
if(n1>=0)
carry += (num1[n1--]-'0');
if(n2>=0)
carry += (num2[n2--]-'0');
sum+=carry;
carry = sum/10;
sum%=10;
stringstream ss;
ss<<sum;
string str;
ss>>str;
res+=str;
}
reverse(res.begin(),res.end());
return res;
}
};