371.用位运算实现加法 Sum of Two Integers
Posted Long Long Journey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了371.用位运算实现加法 Sum of Two Integers相关的知识,希望对你有一定的参考价值。
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
public class Solution {
public int GetSum(int a, int b) {
int sum = 0;
while (b != 0) {
sum = a ^ b;
b = (a & b) << 1;
a = sum;
}
return sum;
}
}
以上是关于371.用位运算实现加法 Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章
leetcode371. Sum of Two Integers
LeetCode之371. Sum of Two Integers
LeetCode 371 Sum of Two Integers