才德的女子

关注

leetcode371. Sum of Two Integers

才德的女子

关注

阅读 65

2022-08-05


这题挺有意思

不用+-算+法

先&:求出需要进位的位

后^:求出除了进位的结果

然后:把进位的那位左移,用上面的结果再去+进位后的结果。

class Solution {  
public:
int getSum(int a, int b) {
while(b)
{
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
};



相关推荐

草原小黄河

[leetcode] 371. Sum of Two Integers

草原小黄河 82 0 0

雨鸣静声

LeetCode //C - 371. Sum of Two Integers

雨鸣静声 45 0 0

迪莉娅1979

【leetcode_medium】371. Sum of Two Integers

迪莉娅1979 69 0 0

转角一扇门

【LeetCode】371. Sum of Two Integers 解题报告(Python)

转角一扇门 51 0 0

一天清晨

LeetCode Top Interview Questions 371. Sum of Two Integers (Java版; Easy)

一天清晨 36 0 0

成义随笔

sum of two integers

成义随笔 81 0 0

罗蓁蓁

LeetCode-1317. Convert Integer to the Sum of Two No-Zero Integers

罗蓁蓁 55 0 0

金刚豆

[LeetCode]Divide Two Integers

金刚豆 123 0 0

落花时节又逢君to

LeetCode Two Sum

落花时节又逢君to 80 0 0

GhostInMatrix

位运算中等 LeetCode371. 两整数之和

GhostInMatrix 34 0 0

精彩评论(0)

0 0 举报