Sum of Two integers

两个整数相加不能用加减

用位运算

假设两整数a=2和b=6,它们的二进制表示分别为010和110

sum=a^b表示两个二进制数相加不考虑进位:

010

^  110

=  100

carry=(a&b)<<1表示两个二进数相加的进位

010

& 110

= 010

<<1

=100

递归地做sum^carry直到carry=0

代码(一行反而比较好理解):

int getSum(int a, int b) {
return b == 0 ? a : getSum(a ^ b, (a & b) << 1);
}

时间: 2025-01-20 03:54:05

Sum of Two integers的相关文章

371. Sum of Two Integers

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. Credits: Special thanks to @fujiaozhu for adding this problem and creating all test cases. 这道题让我们实现两数相加,但是不能用加号或

[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 -. Example:Given a = 1 and b = 2, return 3. 用 异或 表示sum,与 表示 carry,carry左移一位后,递归调用getSum(sum,carry<<1) 一刷: public int getSum(int a, int b) { return b ==0 ? a

LeetCode 371 Sum of Two Integers

This problem is a little confusing to me at first. But after I read some articles online, I got to know that it requires bitwise operations. So basically, we need to use "^" to calculate the sum of two integers, and "&" << 1

371. Sum of Two Integers【位运算】

2017/3/16 20:03:07 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. 思路: 1.^可以得到哪些bit求和后应该是1.(进位的bit应该变成0,1^1=0) 2.&可以得到哪些bit应该进位 3.由于进位后的bit是1可能和第一步得到的1再次产生进位,所以不

371.用位运算实现加法 Sum of Two Integers

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. Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. Subscribe to see which

【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 -. 解题分析: 这种类型的题必然要用位运算,虽然自己写了关于位运算的代码,但是不够简洁. 后来参考了这篇博文: http://blog.csdn.net/zhongjiekangping/article/details/6855864 这篇博文对位运算加法的实现讲的得十分清楚,我这里就只放按此思路写

Leetcode 371. Sum of Two Integers JAVA语言

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. 题意:计算a+b,但是不许使用+和- public class Solution {     public int getSum(int a, int b) {         //第一种         while(a!=

[LeetCode] Sum of Two Integers

The code is as follows. public class Solution { public int getSum(int a, int b) { return b == 0 ? a : getSum(a ^ b, (a & b) << 1); } } The above code may be rewritten to make it more readable. public class Solution { public int getSum(int a, int

Nim Game,Reverse String,Sum of Two Integers

下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take t

**leetcode笔记--4 Sum of Two Integers

question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. my wrong answer 错误点: 显示memory error 应该是运行较大数字时导致内存不够,所以这种方法不佳 正确答案地址:(暂时不明白) http://blog.csdn.net/mebiuw/article/details/51788817 http://www.cnb