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)。

  但有一个问题,就是进位。先算出进位的值(就是在异或条件下,比正确答案小的值),然后再相加,直到没有进位。

 1 public class Solution
 2 {
 3     public int GetSum(int a, int b)
 4     {
 5         int result = a ^ b;
 6         int carry = (a & b) << 1;
 7         if (carry == 0) return result;
 8         return GetSum(result, carry);
 9     }
10 }
时间: 2024-12-10 18:46:02

LeetCode Q371 Sum of Two Integers(Easy)的相关文章

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 retur

【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)上好像也做过这

[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 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] 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. 这道题是CareerCup上的一道原题,难道

LeetCode 第 371 题 (Sum of Two Integers)

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. 不用加减法计算两个整数的和.这道题其实是考察一些基本的布尔代数知识.我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 +

通过位运算求两个数的和(求解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.用乘除代替加减,但是一想,