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 { * ...
随机推荐
- Shell 操作练习
#! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...
- 关于后台管理linkbutton按钮几个重要属性的理解
<asp:LinkButton ID="lkbtnDelete" runat="server" CausesValidation="False& ...
- HTML&CSS----练习做网页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- NBOJv2 Problem 1009 蛤玮的魔法(二分)
Problem 1009: 蛤玮的魔法 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %ll ...
- T-SQL DBMS
dbo 默认架构schema 从一个数据库操作另个数据库的表的时候,要select * from 数据库.dbo.biao 表名前面的dbo是一个默认架构schema,一个架构还有 ...
- Javascript 笔记与总结(2-12)联动菜单
联动菜单: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 【IOS笔记】About Events in iOS
About Events in iOS Users manipulate their iOS devices in a number of ways, such as touching the scr ...
- 动态样式语言Less学习笔记
介绍资料参见:http://www.bootcss.com/p/lesscss/ LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. LESS 既可以在 客户端 上运行 (支 ...
- The world beyond batch: Streaming 101
https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101 https://www.oreilly.com/ideas/the ...
- java Channel filp compact
import java.nio.ByteBuffer; //Listing 7-1. Copying Bytes from an Input Channel to an Output Channel ...