美文网首页
371. Sum of Two Integers

371. Sum of Two Integers

作者: becauseyou_90cd | 来源:发表于2018-07-30 22:31 被阅读0次

https://leetcode.com/problems/sum-of-two-integers/description/

解题思路:
用and operation得到carrry, 用xor operation 得到a,然后对carry左移一位

代码:
class Solution {
public int getSum(int a, int b) {

    if(a == 0) return b;
    while(b != 0){
        int carry = a & b;
        a = a ^ b;
        b = carry << 1;
    }
    return a;    
}

}

相关文章

网友评论

      本文标题:371. Sum of Two Integers

      本文链接:https://www.haomeiwen.com/subject/lamqvftx.html