Leetcode 415 Solution
This article provides solution to leetcode question 415 (add-strings)
Access this page by simply typing in "lcs 415" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/add-strings
Solution
class Solution {
public:
string addStrings(string num1, string num2) {
string res;
auto it1 = num1.rbegin();
auto it2 = num2.rbegin();
int carry = 0;
while (it1 != num1.rend() || it2 != num2.rend())
{
int val = carry;
if (it1 != num1.rend())
val += *it1++ - '0';
if (it2 != num2.rend())
val += *it2++ - '0';
if (val >= 10)
{
val -= 10;
carry = 1;
}
else
carry = 0;
res += to_string(val);
}
if (carry == 1)
res += "1";
reverse(res.begin(), res.end());
return res;
}
};