LintCode A + B Problem
原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/
不让用 数学运算符,就用位运算符。
a的对应位 ^ b的对应位 ^ carry 就是res中的对应位。
carry 更新为0还是1要分别讨论。
Time Complexity: O(1), 一共32位. Space: O(1).
AC Java:
class Solution {
/*
* param a: The first integer
* param b: The second integer
* return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
int res = 0;
int carry = 0;
for(int i = 0; i<32; i++){
int aCur = (a>>i) & 1;
int bCur = (b>>i) & 1;
res |= (aCur ^ bCur ^ carry) << i;
if((aCur == 1 && bCur == 1) || ((aCur == 1 || bCur == 1) && carry == 1)){
carry = 1;
}else{
carry = 0;
}
}
return res;
}
};
LintCode A + B Problem的更多相关文章
- [LintCode] Nuts & Bolts Problem 螺栓螺母问题
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...
- LintCode "Post Office Problem" !!!
* Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an ...
- Lintcode: Nuts & Bolts Problem
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...
- 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 { * ...
随机推荐
- Mysql_mysql force Index 强制索引
其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...
- Git Shell 安装版本
#!/bin/sh v1.; do echo "Begin install Git $ver."; git reset --hard git clean -fdx git chec ...
- iOS开发之UITextField的使用详解
UITextField的使用详解 UITextField控件是开发中,使用频率比较高的控件了,那么有必要总结一下. 一.UITextField手动编写控件 UITextField *txtAccou ...
- https资料
1.HTTPS的七个误解 http://blog.httpwatch.com/2011/01/28/top-7-myths-about-https/ 中文 http://www.cnblogs.c ...
- workerman使用编译安装workerman的php环境
提示 workerman只是一个代码包,如果php环境满足要求,下载后即可使用,实际上没有安装过程. workerman对php环境的要求是: 1.php>=5.3.3,可以运行命令php-v查 ...
- [GE]导入图片至Word,然后按规则命名(2/2)
#将所有docx文件改成可读 Set-ItemProperty -Path "e:\screenshot\*.docx" -Name IsReadOnly -Value $fals ...
- Disaster recovery best practices for Symantec Endpoint Protection 12.1
https://support.symantec.com/en_US/article.TECH160736.html
- MAC环境下 nodejs 4.5.0 卸载
MAC 环境下,我之前的删除 node 环境如果是用brew 安装的 node.js 4.4 之前的版本.可以网上参考其他方法,我之前的帖子也有之前的删除方法.参考帖子: http://www.cnb ...
- node系列:琐碎备忘
cmd 全局与本地路径 查看:默认 查看本地路径:npm config get cache,默认和nodejs安装目录同一目录 查看全局路径:npm config get prefix,默认c盘app ...
- php中ajax跨域请求---小记
php中ajax跨域请求---小记 前端时间,遇到的一个问题,情况大约是这样: 原来的写法: 前端js文件中: $.ajax({ type:'get', url:'http://wan.xxx.c ...