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 ...
随机推荐
- matlab 图像常用函数
Canny function [ canny ] = canny( rgb ) temp=rgb2gray(rgb); canny=edge(temp,'canny'); end 灰度 temp=rg ...
- 在Spark程序中使用压缩
当大片连续区域进行数据存储并且存储区域中数据重复性高的状况下,数据适合进行压缩.数组或者对象序列化后的数据块可以考虑压缩.所以序列化后的数据可以压缩,使数据紧缩,减少空间开销. 1. Spark对压缩 ...
- 用mappedbytebuffer实现一个持久化队列【转】
自从前段时间的一个事故让队列里缓存的大量关键数据丢失后,一直琢磨着弄一个能持久化到本地文件的队列,这样即使系统再次发生意外,我也不至于再苦逼的修数据了.选定使用mappedbytebuffer来实现, ...
- PostgreSql 合并多行记录
需求描述: A表有如下数据 id 1 2 3 4 B表有如下数据 id name 1 aaa 1 bbb 1 ccc 2 aa 2 bb 3 c A表和B表通过id关联,需要查询结果如下: id na ...
- hdu 1024 dp滚动数组
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...
- Atlassian官方合作伙伴
Atlassian官方合作伙伴 http://atlassian.csdn.net/m/btc/atlassian/index
- 给DB2某表增加一个自增长列
如果是MySQL表,那么任务就很简单,一句SQL可以搞定.而DB2表,就要费一点周折了. 首先,我们来看一眼目标表: 这个表,有字段有数据,我们需要做的是,给它加一个唯一性的ID列. 我们可以通过工具 ...
- (剑指Offer)面试题42:左旋转字符串
题目: 字符串的左旋转操作是把字符串前面的若干字符转移到字符串的后面.请定义一个函数实现字符串左旋转操作的功能, 比如:输入字符串"abcdefg"和数字2,该函数将返回左旋转2位 ...
- xcode 调试程序 lldb 使用
xcode 调试程序 lldb 使用 一:lldb是什么 https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/g ...
- Cookie 获取访问时间
服务器将客户端需要缓存的数据,发送到客户端,客户端保存在本地的这些缓存数据就是Cookie.区别于Session. 获取用户访问时间代码: response.setCharacterEncodin ...