Leetcode:371.Sum Of Two Integer

题目:        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进行循环,直到进位为0为止。计算过程与二进制加法器一致。 
 1 public class Solution {
 2     public int getSum(int a, int b) {
 3         while(b!=0){
 4             int carry = a & b; //计算进位
 5             a = a ^ b;         //计算和数
 6             b = carry<<1;
 7         }
 8         return a;
 9     }
10 }
时间: 2025-01-03 01:07:12

Leetcode:371.Sum Of Two Integer的相关文章

[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(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 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]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

[leetcode]Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum =