There was an error while loading. Please reload this page.
1 parent ded6612 commit 9e03fbfCopy full SHA for 9e03fbf
08 August Leetcode Challenge 2021/09_addStrings.cpp
@@ -0,0 +1,21 @@
1
+class Solution {
2
+public:
3
+ string addStrings(string num1, string num2) {
4
+ string ans = "";
5
+ int i1 = num1.size() - 1, i2 = num2.size() - 1, carry = 0;
6
+ while (i1 >= 0 || i2 >= 0 || carry > 0) {
7
+ if (i1 >= 0) {
8
+ carry += num1[i1] - '0';
9
+ i1 -= 1;
10
+ }
11
+ if (i2 >= 0) {
12
+ carry += num2[i2] - '0';
13
+ i2 -= 1;
14
15
+ ans += char(carry % 10 + '0');
16
+ carry /= 10;
17
18
+ reverse(ans.begin(), ans.end());
19
+ return ans;
20
21
+};
0 commit comments