LeetCode: 371 Sum of Two Integers(easy)

题目:

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位来算进位,然后将两者相加。

1 class Solution {
2 public:
3     int getSum(int a, int b) {
4         return b == 0 ? a : getSum(a^b, (a&b)<<1);
5     }
6 };

什么样的数据可以直接移位? char、 short、 int、 long、 unsigned char、 unsigned short、 unsigned int、 unsigned long

什么样的数据不能直接移位? double、 float、 bool、 long double

时间: 2025-01-14 19:20:44

LeetCode: 371 Sum of Two Integers(easy)的相关文章

[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

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 Q371 Sum of Two Integers(Easy)

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,不用加法和减法的情况下算出a+b. 分析: 一看题就知道肯定是于位运算有关,很快想到异或,因为只有异或满足单一位的二进制运算(1^1=0,1^0=1,0^1=1,0^0=0). 但有一个问题,就是进位.

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 -. 思路:两个数的加法分为两步,对应位相加和进位. 举个简单的例子:997+24 我们平时计算时是将对应位相加和进位同时计算,其实可以保留下进位,只计算对应位相加,保留进位的位置(值).接下来,将进位向左移动一位,将上一步的结果与移位后的进位值进行对应位相加,直到没有进位结束. 对于二进制数的而言,对应

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

昨天在leetcode做题的时候做到了371,原题是这样的: 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. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,

【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】Sum of Two Integers

题目链接:https://leetcode.com/problems/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. 思路: 唉,这题虽然是easy,但是真好烦的一题,之前在hihocoder(还是其他oj)上好像也做过这

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. 这道题让我们实现两数相加,但是不能用加号或