Skip to content

Commit

Permalink
Time: 8 ms (65.24%), Space: 6.9 MB (41.09%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Dec 7, 2022
1 parent 05c3a3f commit dc55887
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 0415-add-strings/0415-add-strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
string addStrings(string num1, string num2) {
string ans = "";
int i1 = num1.size() - 1, i2 = num2.size() - 1, carry = 0;
while (i1 >= 0 || i2 >= 0 || carry > 0) {
if (i1 >= 0) {
carry += num1[i1] - '0';
i1 -= 1;
}
if (i2 >= 0) {
carry += num2[i2] - '0';
i2 -= 1;
}
ans += char(carry % 10 + '0');
carry /= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
};

0 comments on commit dc55887

Please sign in to comment.