[LintCode] A + B 问题
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:
- a + b without carry;
- carry of a + b;
- 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 问题的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
- Lintcode 375.克隆二叉树
-------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...
随机推荐
- nginx-1.2.7+tcp_proxy_module负载均衡配置
对于安装方面不清楚的,可移步 nginx + tcp_proxy_module的安装 参考nginx_tcp_proxy_module的readme文件,对每个定义项都做了详细的描述,tcp{...} ...
- unity, 删除animationEvent
当初给Player的一个animation加了个animationEvent,后来与之关联的响应函数删除了,于是导致报错: ‘Player’ AnimationEvent ‘idleHalfEvent ...
- C语言 Linux环境变量
/* *@author cody *@date 2014-08-12 *@description */ /* extern char **environ //environment values #i ...
- Atitit.100% 多个子元素自适应布局属性
Atitit.100% 多个子元素自适应布局属性 1.1. 原理1 1.2. Table布局1 1.3. Css布局1 1.4. 判断amazui加载完毕2 1.1. 原理 每个子元素平均分配,但是有 ...
- C语言中的传值调用
在c语言中每个变量都有两个属性一个是值,一个是址. 比方: int a = 2; 变量a的值是2,变量a的地址,能够用&取地址操作符获取,即&a. 因此以C语言的函数传递中具备两种方式 ...
- 547. Intersection of Two Arrays【easy】
Given two arrays, write a function to compute their intersection. Notice Each element in the result ...
- /proc/version 的生成过程
/proc/version 的生成过程 通常我们cat /proc/version时,会显示kernel相关的版本.编译等信息 那么问题来了,这些信息是怎么生成的呢? /proc/version文件是 ...
- unity shader(二)
- #include""和#include<>的区别
一般来说,就是搜索路径不同 #include ""先去搜用户当前路径(也就是调用编译器的路径),然后再去搜用户用-I选项指定的路径,最后再去搜索编译器默认指定的路径(也就是所谓的系 ...
- spring-common.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...