[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 ...
随机推荐
- Python之基础数学知识
一.线性代数 1.求转置 import numpy m = numpy.mat([[1, 2], [3, 4]]) print("Matrix.Transpose:") print ...
- Ant—使用Ant构建简单Java项目(三)
博客<Ant-使用Ant构建简单Java项目(二)>我们简化了运行Test类中main方法须要运行的命令,本博客来介绍一下如何使build.xml文件和当中使用property标签定义的属 ...
- mysql安装在centos7报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
原文链接:http://blog.csdn.net/kuluzs/article/details/51924086 [问题]:mysql版本:5.7.13 首次在centos下安装MySQL,客户端连 ...
- zookeeper(四):核心原理(Watcher、事件和状态)
zookeeper主要是为了统一分布式系统中各个节点的工作状态,在资源冲突的情况下协调提供节点资源抢占,提供给每个节点了解整个集群所处状态的途径.这一切的实现都依赖于zookeeper中的事件监听和通 ...
- [MySQL] Innodb參数优化
innodb_buffer_pool_size innodb_buffer_pool_size 參数用来设置Innodb 最基本的Buffer(Innodb_Buffer_Pool)的大小,也就是缓存 ...
- hdu 1711 Number Sequence KMP 基础题
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- CentOS下安装man手册
1.命令: yum install man 后发现,有的函数仍然没有.经过一番查找,原来安装的不完全,还要执行下面命令: yum install man-pages 2.总结:cengos下安装man ...
- [gj]来公司停电了-维护机器
停电出现的问题 一早来机房跳闸了. 很多vm手动起来后有些服务不太正常了. 机房锁坏了.不知道谁进去干啥了.导致跳闸 闸门不知道在哪里.导致所有机器停电了 牵扯到的技术点: 1,nexus启动. ng ...
- mysql数据库分库备份脚本
mysql数据库分库备份脚本 版本1 for dbname in `mysql -uroot -poldboy123 -e "show databases;" |grep -Evi ...
- Java创建多线程的三种方法
Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...