Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db8feb2

Browse filesBrowse files
Merge pull request FazeelUsmani#128 from FazeelUsmani/add-string
Create 09_addStrings.cpp
2 parents 054243a + 9e03fbf commit db8feb2
Copy full SHA for db8feb2

File tree

Expand file treeCollapse file tree

1 file changed

+21
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-0
lines changed
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.