原题链接在这里: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的更多相关文章

  1. [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 ...

  2. LintCode "Post Office Problem" !!!

    * Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an ...

  3. 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 ...

  4. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  5. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  6. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  7. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  8. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

  9. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

随机推荐

  1. Shell 操作练习

    #! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...

  2. 关于后台管理linkbutton按钮几个重要属性的理解

    <asp:LinkButton ID="lkbtnDelete" runat="server" CausesValidation="False& ...

  3. HTML&CSS----练习做网页

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. NBOJv2 Problem 1009 蛤玮的魔法(二分)

    Problem 1009: 蛤玮的魔法 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %ll ...

  5. T-SQL DBMS

    dbo 默认架构schema 从一个数据库操作另个数据库的表的时候,要select * from 数据库.dbo.biao           表名前面的dbo是一个默认架构schema,一个架构还有 ...

  6. Javascript 笔记与总结(2-12)联动菜单

    联动菜单: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. 【IOS笔记】About Events in iOS

    About Events in iOS Users manipulate their iOS devices in a number of ways, such as touching the scr ...

  8. 动态样式语言Less学习笔记

    介绍资料参见:http://www.bootcss.com/p/lesscss/ LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. LESS 既可以在 客户端 上运行 (支 ...

  9. The world beyond batch: Streaming 101

    https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101 https://www.oreilly.com/ideas/the ...

  10. java Channel filp compact

    import java.nio.ByteBuffer; //Listing 7-1. Copying Bytes from an Input Channel to an Output Channel ...