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 ...
随机推荐
- linux查看进程信息 so依赖
查看进程ps -A PID进程的sopmap -x pid
- Single Number and Single Number II
[] Given an array of integers, every element appears twice except for one. Find that single one. [] ...
- ASP.NET 5 的Roadmap(转)
这次随 Visual Studio 2015 发布的 ASP.NET 版本是 ASP.NET 4.6 与 ASP.NET 5 beta5.在 VS2015 发布的同时,微软也发布了 ASP.NET 5 ...
- OpenMP 中的线程任务调度
OpenMP中任务调度主要针对并行的for循环,当循环中每次迭代的计算量不相等时,如果简单地给各个线程分配相同次数的迭代,则可能会造成各个线程计算负载的不平衡,影响程序的整体性能. 如下面的代码中,如 ...
- 使用C# + httpWebRequest 解析WMTS服务元数据
解析http://219.142.81.86/igserver/ogc/kvp/TAS10R52000J49/WMTSServer服务的元数据 // 测试httpWebRequest读取 wmts元数 ...
- C#类的访问修饰符
默认情况下,类声明为内部的,即只有当前工程中的代码才能访问它.可以用internal访问修饰符关键字显式指定,但这不是必须的,类在定义时默认为此类型的类.但是C# 方法默认访问级别: private. ...
- c#中this的用法
在C#中,this关键字代表当前实例,我们可以用this.来调用当前实例的成员方法,变量,属性,字段等; 也可以用this来做为参数状当前实例做为参数传入方法. 还可以通过this[]来声明索引器 下 ...
- java中 HashMap和Hashtable,list、set和map 的区别
摘自: http://blog.chinaunix.net/uid-7374279-id-2057584.html HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Ma ...
- MySQL服务器安装完之后如何调节性能
原文作者: Peter Zaitsev原文来源: http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server ...
- (转)Apache的安装与配置
转自:http://www.dreamdu.com/webbuild/apache/ 5.2. Apache的安装与配置 5.2.1. Apache安装与配置视频教程 下面将介绍如何在WinXP下安装 ...