Bit-by-Bit summation:

 class Solution {
public:
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
int res = , sum = , carry = ;
for (int i = ; i < ; i++, a >>= , b >>= ){
int d1 = a & , d2 = b & ;
sum = (d1 ^ d2 ^ carry);
carry = max((d1 & d2), max((d1 & carry), (d2 & carry)));
res ^= (sum << i);
}
return res;
}
};

Treat a + b as two parts:

  1. a + b without carry;
  2. carry of a + b;
  3. recursively plus part 1 and part 2 until no carry exists.
 class Solution {
public:
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
while (b) {
int carry = a & b; // carry
a ^= b; // plus without carry
b = carry << ; // recursively plus the two parts
}
return a;
}
};

The code can be further shortened by writing it recursively.

 class Solution {
public:
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
if (!b) return a;
return aplusb(a ^ b, (a & b) << );
}
};

Or just in one line :-)

 class Solution {
public:
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
return (!b ? a : aplusb(a ^ b, (a & b) << ));
}
};

[LintCode] A + B 问题的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  3. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  4. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  5. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  6. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  7. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

  8. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  9. Lintcode 469. 等价二叉树

    ----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...

  10. Lintcode 375.克隆二叉树

    -------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...

随机推荐

  1. java中finalkeyword使用说明

    必须在域的定义处或者每一个构造器中用表达式对final进行赋值,这正是final域在使用前总是被初始化的原因所在.

  2. FZU 2087 统计树边【MST相关】

     Problem 2087 统计树边 Accept: 212    Submit: 651 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  3. unity, itween 对不透明对象使用FadeTo需要先更换material

    跟自己实现fade一样,使用itween对不透明对象FadeTo前也要先更换material为透明material. 设player的Hierarchy如下: player --aniRoot --- ...

  4. 阿里大鱼短信接口(Python3版)

    近期由于须要用到短信接口,选型的的结果是用阿里大鱼的短信服务,然而淘宝开放平台(TOP)的SDK已经非常多年没有更新了.不支持python3.自己动手改了半天,还是不太正常,索性不用它,自己写一个算了 ...

  5. 150. Best Time to Buy and Sell Stock II【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. CefSharp 集成谷歌浏览器详解---(一)环境搭建(没测试过,不知道好不好用)

    https://blog.csdn.net/zpyxman/article/details/78538808

  7. 问题-Delphi7中JSON遍历节点不支持使用IN处理方法?

    相关资料:http://www.cnblogs.com/del/archive/2009/10/23/1588690.html 问题现象:在高版本中可以使用IN处理JSON的节点循环问题,可是发现D7 ...

  8. Tomcat 下启用 https:// 访问

    步骤: 1 创建 .keystore 文件 JDK中自带了keytool工具用于生成证书文件 keytool工具在$JAVA_HOME/bin 目录下可以使用命令 keytool -genkey -a ...

  9. 在GOOGLE浏览器中模拟移动浏览器 调试Web app

    在此记录下,以便在今后的工作中用到. 首先通过F12 or Ctrl+Shift+i,打开开发者工具,点击开发者工具面板的 (show  drawer)按钮,出现如下图所示的面板: 切换至Emulat ...

  10. Nexus 5 刷机 - Android 5.0 Lollipop

    Nexus刷机 : 官方地址 刷机步骤 下载相应的安装包 连接USB 重启手机,进入BootLoader界面 : 使用命令 adb reboot bootloader 关机; 音量键下 + 电源键 ...