Skip to content

Commit 928f7a3

Browse files
committed
371
1 parent 3cf393b commit 928f7a3

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Leetcode solutions in C++, Java, Python
33

44
|#|Title|Solution|Difficulty|
55
|---|-----|--------|----------|
6+
|371|[Sum of Two Integers](./src/sum_of_two_integers)|[C++](./src/sum_of_two_integers)|Easy|
67
|370|[Range Addition](./src/range_addition)|[C++](./src/range_addition)|Medium|
78
|369|[Plus One Linked List](./src/plus_one_linked_list)|[C++](./src/plus_one_linked_list)|Medium|
89
|368|[Largest divisible subset](./src/largest_divisible_subset)|[C++](./src/largest_divisible_subset)|Medium|

src/sum_of_two_integers/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all:
2+
clang++ main.cpp -o main.o -std=c++11
3+
4+
run:
5+
clang++ main.cpp -o main.o -std=c++11
6+
./main.o

src/sum_of_two_integers/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Problem
2+
===
3+
late the sum of two integers a and b, but you are not allowed to use the operator + and -.
4+
5+
Example:
6+
Given a = 1 and b = 2, return 3.
7+

src/sum_of_two_integers/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <algorithm>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <map>
5+
#include <queue>
6+
#include <set>
7+
#include <sstream>
8+
#include <stack>
9+
#include <string>
10+
#include <unordered_map>
11+
#include <unordered_set>
12+
#include <vector>
13+
14+
using namespace std;
15+
16+
class Solution {
17+
public:
18+
int getSum(int a, int b) {
19+
int x = a, y = b;
20+
while (y != 0) {
21+
int xx = x ^ y;
22+
int yy = x & y;
23+
x = xx;
24+
y = yy << 1;
25+
}
26+
return x;
27+
}
28+
};
29+
30+
int main() {
31+
Solution sol;
32+
33+
cout << sol.getSum(1, 2) << endl;
34+
35+
cout << sol.getSum(2, 2) << endl;
36+
37+
cout << sol.getSum(3, 5) << endl;
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)