码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode: A + B Problem

时间:2015-11-26 21:12:39      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

C++

 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         int sum = a^b;
11         int carry = (a&b) << 1;
12         int tmp;
13         while (carry) {
14             tmp = sum;
15             sum = tmp^carry;
16             carry = (tmp&carry) << 1;
17         }
18         return sum;
19     }
20 };

 

LintCode: A + B Problem

标签:

原文地址:http://www.cnblogs.com/CheeseZH/p/4998708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!