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 { * ...
随机推荐
- li:hover在ie6下失效的解决方案
li:hover在ie6下是无效的,它只在ie7以下版本有效.要解决这个问题有两个解决方法.一个是用js来解决,但是这种方法我不喜欢,因为它必需把js代码和css代码都放在html文件中.第二种是在每 ...
- NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...
- System.getProperty
我们可以通过System.getProperty("user.home")读取JAVA系统的user.home属性的值.
- HTML练习----注册界面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Leetcode | Valid Sudoku & Sudoku Solver
判断valid,没有更好的方法,只能brute force. class Solution { public: bool isValidSudoku(vector<vector<char& ...
- ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决
原因是view文件下面有个.htaccess文件,里面写的是 Deny from all //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...
- git 远程仓库ssh方式
用ssh-keygen生成公匙和私钥 d:\c\learnc>ssh-keygen Generating public/private rsa key pair. Enter file in w ...
- linux消息队列的使用及内核实现原理
mq_receive NAME mq_open - open a message queue SYNOPSIS #include <fcntl.h> /* For O_* constant ...
- PHP 对象和引用总结
PHP 中使用 简单变量 和 对象 时的区别: ① 很多数据类型都可以写时复制(copy-on-write),例: <?php $a = 'test1'; $b = $a; $b = 'test ...
- mysql 关于日期时间的字段类型
mysql日期和时间类型 mysql有5种表示时间值的日期和时间类型,分别为.DATE,TIME,YEAR,DATETIME,TIMESTAMP. TIMESTAMP类型有专有的自动更新特性, TIM ...