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. 代码规范审查 – Sonar分析项目

    Sonar搭建成功之后,就可以通过简单的CMD指令进行项目分析,此篇主要介绍使用Sonar来分析.net项目. 扫描步骤: a.    打开CMD,切换到指定的项目根目录,和.sln同级目录即可(此处 ...

  2. Django的restful api三方包:djangorestframework

    文档位置:https://www.django-rest-framework.org/ 代码位置:https://github.com/encode/django-rest-framework/tre ...

  3. HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。

    IIS的根节点->右侧"ISAPI和CGI限制"->把禁止的DotNet版本项设置为允许,即可.

  4. go语言基础之普通函数的调用流程

    函数调用流程:先调用后返回,先进后出,函数递归,函数调用自己本分,利用此物点 1.普通函数的调用流程 package main //必须 import "fmt" func fun ...

  5. Node.js:EventEmitter类

    一.EventEmitter 类 Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时 ...

  6. 搜索引擎爬虫蜘蛛的useragent

    百度爬虫    * Baiduspider+(+http://www.baidu.com/search/spider.htm”) google爬虫    * Mozilla/5.0 (compatib ...

  7. zend studio 13.0.0 安装破解汉化

    zend studio 13安装破解汉化步骤 官网原版下载 http://downloads.zend.com/studio-eclipse/13.0.0/ZendStudio-13.0.0-win3 ...

  8. Python模块学习 --- urllib

    urllib模块提供的上层接口,使我们可以像读取本地文件一样读取www和ftp上的数据.每当使用这个模块的时候,老是会想起公司产品的客户端,同事用C++下载Web上的图片,那种“痛苦”的表情.我以前翻 ...

  9. [转发]using的几种用法

    1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...

  10. Win8多平台引用配置

    之前移植过DLNA的库,这个库是C++写的,然后我们的项目是C#的.接着很郁闷的事情发生了,主项目引用一个C#的DLL,然后这个DLL引用这个C++/CX封装的库.如果有C++的源代码的话,做项目依赖 ...