# LeetCode之Sum of Two Integers

### 1、题目

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

#### Example:

Given a = 1 and b = 2, return 3.

Credits:

Special thanks to @fujiaozhu for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

### 2、代码实现

public class Solution {

public int getSum(int a, int b) {

while(b != 0)

{

int tmp =(a & b) << 1;

//a是每次相加,相当于和

a = a ^ b;

//b相当于是进位

b = tmp;

}

return a;

}

}

|
11月前
|

LeetCode刷题---Two Sum（一）
LeetCode刷题---Two Sum（一）
|
9月前
Leetcode 4. Median of Two Sorted Arrays

21 0
|
11月前
|

|

LeetCode - #2 Add Two Numbers

64 0
|

LeetCode - #1 Two Sum

48 0
LeetCode 167 Two Sum II - Input array is sorted（输入已排序数组，求其中两个数的和等于给定的数）

74 0
|
Python
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
81 0
|

LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
95 0
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
88 0
|

LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
105 0