-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path415.addStrings.cpp
More file actions
55 lines (51 loc) · 1.09 KB
/
Copy path415.addStrings.cpp
File metadata and controls
55 lines (51 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
@filename string/415.addStrings.cpp
@author caonan
@date 2023-09-09 11:00:47
@reference leetcode
@url https://leetcode.cn/problems/add-strings/description/
@brief 字符串操作
*/
#include <cassert>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
string addStrings(string num1, string num2) {
string ans;
int carry = 0;
int i = num1.size() - 1;
int j = num2.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int x = i >= 0 ? num1[i] - '0' : 0;
int y = j >= 0 ? num2[j] - '0' : 0;
int sum = x + y + carry;
ans.push_back(sum % 10 + '0');
carry = sum / 10;
i--, j--;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
int main() {
string a = "123";
string b = "45";
Solution s;
auto ret = s.addStrings(a, b);
cout << ret << endl;
return 0;
}