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

Sign up
Appearance settings

Commit d7d92d7

Browse files
add: Sum of Two Integers
1 parent 4f4f837 commit d7d92d7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4444
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
4545
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [JavaScript](./src/power-of-four/res.js)|Easy|
4646
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [JavaScript](./src/reverse-string/res.js)|Easy|
47+
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [JavaScript](./src/sum-of-two-integers/res.js)|Easy|
4748
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [JavaScript](./src/shuffle-an-array/res.js)|Medium|
4849
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [JavaScript](./src/sum-of-left-leaves/res.js)|Easy|
4950
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/) | [JavaScript](./src/number-of-segments-in-a-string/res.js)|Easy|

‎src/sum-of-two-integers/res.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017年03月03日 22:05:30
5+
*
6+
* Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
7+
*
8+
* Example
9+
*
10+
* Given a = 1 and b = 2, return 3.
11+
*
12+
* @param {number} a
13+
* @param {number} b
14+
* @return {number}
15+
*/
16+
let getSum = function(a, b) {
17+
if (b === 0) {
18+
return a;
19+
}
20+
21+
while (b !== 0) {
22+
let t = a^b;
23+
b = (a&b) << 1;
24+
a = t;
25+
}
26+
27+
return a;
28+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /