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. matlab在图像中画长方形(框)

    function [state,result]=draw_rect(data,pointAll,windSize,showOrNot) % 函数调用:[state,result]=draw_rect( ...

  2. C++keyword大总结

    register: 假设有一些变量使用频繁,则为存取变量的值少花一些时间, 能够将该局部变量的值放在CPU的寄存器中,须要时直接从寄存器 中取出參加运算,不必去内存中去存取. 由于寄存器的存取速度 远 ...

  3. OpenCV学习(2) OpenCV的配置

          下面我们在VS2010中编写一个简单的OpenCV程序,来看看如何在程序中使用OpenCV. 创建一个新的Win32 控制台程序,附加选项为空工程(empty project),并添加一个 ...

  4. [Todo]对于thrift和protobuf比较好的描述

    比较跨语言通讯框架:thrift和Protobuf 全部thrift protobuf 前两天想在微博上发表一个观点:在现在的技术体系中,能用于描述通讯协议的方式很多,xml,json,protobu ...

  5. Orchard运用 - 整合多说评论插件

    曾经我在一随笔讲述如何整合第三方Disqus评论插件,不过这一插件不是本土,对中国客户毕竟有点别扭.比如这一随笔就提到为啥要选择多说 - 另外一个国内比较知名的评论插件. 今天跟大家分享如何用最简单的 ...

  6. ES6 中的 Set

    收录待用,修改转载已取得腾讯云授权 作者:kurtshen ES6 新增了几种集合类型,本文主要介绍Set以及其使用. 其基本描述为 Set对象是值的集合,你可以按照插入的顺序迭代它的元素. Set中 ...

  7. ZOJ 3630 Information 强连通

    题意:n m表示n个节点,m条边,下面m行a b 表示a-b点有一条有向边 题目:给定有向图,删去一个点后,可以求出该图中强连通分量中最大的点数 问:删去某点后,最大点数 最小是多少 思路:枚举删点, ...

  8. 算法笔记_009:字符串匹配(Java)

    1 问题描述 给定一个n个字符组成的串(称为文本),一个m(m <= n)的串(称为模式),从文本中寻找匹配模式的子串. 2 解决方案 2.1 蛮力法 package com.liuzhen.c ...

  9. 百度地图API拾取坐标网址

    http://api.map.baidu.com/lbsapi/getpoint/index.html

  10. 最低位 【杭电-HDOJ-1196】 附题

    /* Lowest Bit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...