Write a function that add two numbers A and B. You should not use + or any arithmetic operators.

Notice

There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and return it.

 
Clarification

Are a and b both 32-bit integers?

  • Yes.

Can I use bit operation?

  • Sure you can.
Example

Given a=1 and b=2 return 3

Challenge

Of course you can just return a + b to get accepted. But Can you challenge not do it like that?

解法一:

 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) {
while (b != ) {
int aa = a ^ b;
int bb = (a & b) << ;
a = aa;
b = bb;
} return a;
}
};

 

1. A + B Problem【easy】的更多相关文章

  1. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  2. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  3. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  4. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  5. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

  6. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  7. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  8. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  9. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

随机推荐

  1. ContentProvider的那些小事(纯结论)

    一.ContentProvider背景 Android系统是基于Linux系统内核来进行开发的,在Linux中,文件具有一系列的属性,其中最重要的莫过于文件权限了.关于文件权限,其实就是文件的读写,执 ...

  2. react组件引用时的default常见错误

    1.下面使用方法是正确的: export class StepLoad extends React.Component {} 引用上面插件的方法,重命名 import {StepLoad as Ste ...

  3. 微软URLRewriter.dll的url重写的简单使用(实现伪静态)

    先添加引用URLRewriter.dll到项目下的bin目录中,下载: http://files.cnblogs.com/tianguook/URLRewriter.rar 1.在web.config ...

  4. ViewPager Fragment PagerAdapter MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. javascript常用技巧归纳

    最近归纳了下,发现还很多的哦1 javascript捕捉方向键  <HTML><HEAD><title>反选</title><script lan ...

  6. PlSQL Oracle 中的 对应 SQL server 中的 IsNull(Expr1,Expr2)

    NVL(Expr1,Expr2)如果Expr1为NULL,返回Expr2的值,否则返回Expr1的值NVL2(Expr1,Expr2,Expr3)如果Expr1为NULL,返回Expr2的值,否则返回 ...

  7. 使用Intellij加载Spark源代码

    如何使用Intellij加载Spark源代码 转载注明原文http://www.cnblogs.com/shenh062326/p/6189643.html 查看Spark源代码或修改Spark源代码 ...

  8. js 能实现监听F5页面刷新子iframe 而父页面不刷新

    重点是阻止默认的刷新,这样外部页面就不刷新了,然后指定刷新iframe,我下面给出了思路 document.onkeypress = function(e){ if(e.keyCode == 116) ...

  9. iPad Air PSD设计模板

    免费下载 免费的ipad air PSD 设计,包含了3个不同颜色的外套的设计. 提供免费下载! 阅读原文:iPad Air PSD设计模板

  10. cocos2dx坐标系

    原文转载:http://blog.csdn.net/ejet_shen/article/details/17327223 cocos2dx坐标系 支持下面几种坐标系: 1.屏幕坐标系 原点在左上角,X ...