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 ...
随机推荐
- #include <NOIP2008 Junior> 双栈排序 ——using namespace wxl;
题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...
- ThreadLocal实现session中用户信息 的线程间共享
转载自:http://blog.sina.com.cn/s/blog_4b5bc01101013gok.html ThreadLocal并不难理解,我总结的最简单的理解就是: ThreadLocal像 ...
- NYOJ 8 一种排序(comparator排序)
一种排序 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都 ...
- 【python】Django设置SESSION超时时间没有生效?
按手册和网上的方法在settings.py中设置“SESSION_COOKIE_AGE” 和 “SESSION_EXPIRE_AT_BROWSER_CLOSE” 均不生效. 通过查看django的源代 ...
- Jmeter--google plugin插件监控被測系统资源方法
一.插件准备 1.插件下载地址 http://jmeter-plugins.org/downloads/all/ 下面有两个版本号的.1.1.2和1.1.3.注意Jmeter版本号 1.1.2支持Jm ...
- ASCII对比表
ASCII控制字符和ASCII可显示字符 ASCII控制字符 二进制 十进制 十六进制 缩写 能够显示的表示法 名称/意义 0000 0000 0 00 NUL ␀ 空字符(Null) 0000 00 ...
- 网络地址转换相关函数使用(inet_addr,inet_ntoa,inet_addr)
aa 相关函数原型及参数类型: 函数原型:int inet_aton(const char *cp, struct in_addr *inp); in_addr_t inet_addr(const c ...
- [置顶] ArcGIS Runtime SDKs 10.2 for iOS & Android& OS X发布
我们高兴的宣布:ArcGISRuntime SDKs 10.2 for iOS & Android & OS X正式发布!在10.2版本中,你可以在iOS.Android和Mac设备上 ...
- UNIX网络编程调试工具:tcpdump、netstat和lsof
tcpdump程序 tcpdump一边从网络读入分组一边显示关于这些分组的大量信息.它还能够只显示与所指定的准则匹配的那些分组. netstat程序 netstat服务于多个目的: (1)展示网络端点 ...
- java反射-获取方法信息
例子代码如下: package com.reflect; import java.lang.reflect.Method; public class ClassUtill { /* * 打印类的信息, ...