LeetCode——Sum of Two Integers

Question

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:

Given a = 1 and b = 2, return 3.

Solution

异或操作有累加的效果,但是没有进位的效果。什么时候进位呢? 同时为1的时候进位,往前进一位,所以用交集操作,再移位,表示进位。

Answer

class Solution {
public:
int getSum(int a, int b) {
int sum = a;
while(b != 0) {
sum = a ^ b;
b = (a & b) << 1;
a = sum;
}
return sum;
}
};

LeetCode——Sum of Two Integers的更多相关文章

  1. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  2. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  3. Leetcode: Sum of Two Integers && Summary: Bit Manipulation

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  4. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  6. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  7. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  8. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  9. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

随机推荐

  1. 《Sqlserver》Javaweb项目链接sqlserver 2008R2时出现的一系列的错误

    好久没有弄java,玩eclipse了,最近因为小小的原因,参加一个比赛,不得不把javaweb的东西又捡起来,所以不熟悉,再加上之前链接数据库都是用Oracle的,现在公司的电脑上又只是安装了sql ...

  2. c itoa和atoi

    #include <iostream> using namespace std; int main() { #if 1 ; ];//不要写成char*,因为没有分配空间 itoa(num, ...

  3. pandas 读取文件

    import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('G:timeCompare.txt', sep=' ', ...

  4. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

  5. 【BZOJ4456】[Zjoi2016]旅行者 分治+最短路

    [BZOJ4456][Zjoi2016]旅行者 Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北的道路,这些道路两两相交形 ...

  6. iOS tabbar 属性

    1.设置tabbar背景颜色 NSArray *controllers = [NSArray arrayWithObjects:nav_main,nav_channle,nav_me, nil]; _ ...

  7. Backtracking is a form of recursion.

    w https://www.cis.upenn.edu/~matuszek/cit594-2012/Pages/backtracking.html Starting at Root, your opt ...

  8. 短URL DH 密钥交换算法

    w  追问:0-短URL 的时效性,(比如微信个人账户的永久二维码和群的约7天时效二维码):1-0中的时效性对于算法选择的影响,比如简单的HAS映射.sha1.md5......   https:// ...

  9. T-SQL with关键字 with as 递归循环表

    )SET @OrgId = N'901205CA-6C22-4EE7-AE4B-96CC7165D07F'; WITH Childs AS ( SELECT * FROM HROrgRelation ...

  10. python imageio 图片生成gif

    #!/bin/python3 import matplotlib.pyplot as plt import imageio,os TIME_GAP=0.075 #两帧之间的时间间隔,秒为单位 FILE ...