LintCode: A + B Problem
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 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。