1. A + B Problem【easy】
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.
Are a and b both 32-bit integers?
- Yes.
Can I use bit operation?
- Sure you can.
Given a=1 and b=2 return 3
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】的更多相关文章
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 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 ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 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 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- 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 ...
- 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 ...
随机推荐
- JOIN与EXISTS(子查询)的效率研究
使用MySQL提供的Sample数据库Sakila 现将profiling打开,用来一会查看sql执行时间 set profiling=1; exists 子查询与 join联接效率的对比,功能:查看 ...
- SWIG 快速入门
SWIG 安装 本文使用了 SWIG 版本 2.0.4(参见 参考资料 获取下载站点的链接).要构建和安装 SWIG,可按照典型的开源安装流程,在命令提示符下输入以下命令: 请注意,为前缀提供的路径必 ...
- 如何在android模拟器中导入搜狗输入法?
1.下载输入法程序,如:sogouinput_android_1.6_sweb.apk 2.然后cmd进入sdk的tools(有的是platform-tools)目录,输入adb install C: ...
- Android触控屏幕Gesture(GestureDetector和SimpleOnGestureListener的使用教程) 分类:Androidandroid实例
1.当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vie ...
- C++primer习题--第4章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,转载请注明源地址. [习题 4.7] 编写必要的代码将一个数 ...
- (剑指Offer)面试题42:左旋转字符串
题目: 字符串的左旋转操作是把字符串前面的若干字符转移到字符串的后面.请定义一个函数实现字符串左旋转操作的功能, 比如:输入字符串"abcdefg"和数字2,该函数将返回左旋转2位 ...
- (C++)函数参数传递中的一级指针和二级指针
主要内容: 1.一级指针和二级指针 2.函数指针传递的例子 3.什么时候需要传递二级指针? 4.二级指针在链表中的使用 1.一级指针和二级指针 一级指针:即我们一般说的指针,就是内存地址: 二级指针: ...
- .NET破解之爱奇迪(三)
本教程只能用于学习研究,不可进行任何商业用途.如有使用,请购买正版,尊重他人劳动成果和知识产权! .NET破解之爱奇迪(一) .NET破解之爱奇迪(二) 一打开软件,就看到各种注册和未注册提示信息,就 ...
- installshield12如何改变默认安装目录
installshield12如何改变默认安装目录 androidstudio添加其他项目中的module androidrecyclerviewonBindViewHolder [RealSense ...
- JSTL核心标签
JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...