[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 ...
随机推荐
- unity3d对象的克隆
对象克隆,需要有一个预制对象. 首先创建一个预制对象,将其命名为earth,并将材质包附着上去,如下图所示: 然后点住“earth”拖入Assets>Prefabs文件夹,此时earth文字变成 ...
- unity3d控制主摄像头移动
这篇很水,因为就只有一行代码==: Camera.main.transform.Translate (Vector3.forward); 这行代码控制主摄像头向前移动,其它的如下: up:向上 dow ...
- linux系统常用命令 -设置文件夹读写权限
设置文件夹的读写权限: sudo chmod -R 777 /data 权限码描述 sudo chmod 600 ××× (只有所有者有读和写的权限)sudo chmod 644 ××× (所有者有读 ...
- <<Python基础教程>>学习笔记 | 第11章 | 文件和素材
打开文件 open(name[mode[,buffing]) name: 是强制选项,模式和缓冲是可选的 #假设文件不在.会报以下错误: >>> f = open(r'D:\text ...
- OSI7层网络模型
物理层在OSI参考模型中,物理层(Physical Layer)是参考模型的最低层,也是OSI模型的第一层.物理层的主要功能是:利用传输介质为数据链路层提供物理连接,实现比特流的透明传输.物理层的作用 ...
- Bash中的括号(二)
双方括号[[]]的用法: 1.先举个例子说明: 假如你要再判断某个目录是否存在,又想当然写成: $ [ -f exists.txt && -d exists_folder ]; ech ...
- atitit.无为而治在企业管理,国家治理,教育领域的具体思想与实践
atitit.无为而治在企业管理,国家治理,教育领域的具体思想与实践 1. 什么是无为而治 1 2. 无为而治的三个原则 1 3. 抓大放小 1 4. 治理国家 2 5. 企业管理 2 6. 教育领域 ...
- C#.NET中使用BackgroundWorker在模态对话框中显示进度条
这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条. 首先是主窗体代码: using System; using System.Collect ...
- python学习之socket模块
socket.socket(family=AF_INET,type=SOCK_STREAM,proto=,fileno=None) 使用给定的地址族,套接字类型和协议号创建一个新的套接字.family ...
- 从A页面带参数跳转到B页面;进行解析,并显示数据,进行编辑
A页面跳转时候的地址: parent.layer.open({ type: 2, title:'新建草稿', shadeClose: true, shade: 0.8, scrollbar: fals ...