首页 > 其他 > 详细

LeetCode之371. Sum of Two Integers

时间:2016-10-24 07:35:12      阅读:120      评论:0      收藏:0      [点我收藏+]

技术分享

----------------------------------

 

使用位运算实现加法:

a^b 加不同部分

(a&b)<<1 加相同部分

递归相加

 

AC代码:

public class Solution {
    public int getSum(int a, int b) {
        if(b==0) return a;
        int t1=a^b;
        int t2=(a&b)<<1;
        return getSum(t1,t2);
    }
}

 

题目来源: https://leetcode.com/problems/sum-of-two-integers/

LeetCode之371. Sum of Two Integers

原文:http://www.cnblogs.com/cc11001100/p/5991638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!