首页 > 其他 > 详细

【Leetcode】Sum of Two Integers

时间:2016-06-30 16:31:39      阅读:244      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode.com/problems/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.

思路:
唉,这题虽然是easy,但是真好烦的一题,之前在hihocoder(还是其他oj)上好像也做过这题? 每次都是模拟32位运算然后遇到负数就搞不定了。。。参考别人的做法。

算法:

     public int getSum(int a, int b) {
       int c = 0;
       while(b!=0){
           c= a^b; //add
           b = (a&b)<<1;//carry
           a =c;
       }
       return a;
    }

【Leetcode】Sum of Two Integers

原文:http://blog.csdn.net/yeqiuzs/article/details/51789987

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