【leetcode】371. Sum of Two Integers【E】

16、Arduino MEGA 2560 固件与电路详解 本文详细介绍了Arduino MEGA 2560的固件编程与电路设计,涵盖ATmega16U2和ATmega2560的熔丝配置、stk500v2引导加载程序的备份与恢复方法、使用Microchip Studio通过ICSP烧录固件的步骤,以及debugWIRE和JTAG调试接口的选择与配置。同时提供了电路块图解析、组件位置参考及常见问题解决方案,帮助用户深入理解硬件结构并进行高效开发与调试。 阅读详情

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 companies asked this question

python 的方法有点儿臭不要脸了哈哈哈


还是说正经的,java的


Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc).

Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.

首先,先对数据a,b进行&(与)运算,原因是下面用^(异或)的方法来进行加法会漏掉进位,所以,先对数据进行&运算得到carry,carry中为1的位是会进行进位的位,接下来对数据进行^运算,结果记为add1,实现伪加法,之所以是伪加法,是因为它漏掉了进位。那漏掉的进位怎么办呢?对carry进行左移得到C,C+add1就是两个数据的真正的和。那怎么实现C+add1呢?将add1赋予a,C赋予b,重复以上的操作,直到b等于0.


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



class Solution(object):
    def getSum(self, a, b):
        r = [a,b]

        return sum(r)


卸载python弹框报错:Could not set file security for file ‘D:\Config.Msi\abd5.rbf‘. Error: 5. Verify...如何解决? 🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。 阅读详情

相关推荐

龙虾 Skill 技能库|OpenClaw+Hermes 全集成 一键调用所有 AI 技能

龙虾 Skill 技能库、OpenClaw、Hermes Agent、AI 技能集成、本地 AI 自动化、办公工具、数字员工

weixin_42219287的博客 624

371. Sum of Two Integers(两数之和)——————附带详细解析和代码

文章目录0 效果1 题目2 思路3 代码 0 效果 1 题目 2 思路 使用补码进行位运算(因为补码表示法中的加法和减法可以统一使用加法操作实现)。 补充知识: 1,正数的补码和原码相同,负数的补码等于对原码种的数值部分按位取反后末位加1。 2,有符号的移位都是算术移位。 3,算术移位对于正数的补码是补0,对于负数是左移添0,右移添1。 4,补码:最高位为符号位,0表示正数,1表示负数。 5,减法用加法实现:[x-y]补=[x]补-[y]补=[x]补+[-y]补 6,补码运算中,符号位参与运算,符号位

韩江雪de 小屋 763

静止无功补偿SVG的基本控制原理

上图是自励单变量控制框图,基本控制方式为,采集计算得到负载无功电流和SVG输出的无功电流的差值,经过PI调节作为功率角δ和固定的调制深度λ和调制波,经过PWM调制形成开关信号驱动SVG的功率电路。该控制方式使保持调制深度λ(即SVG电压的幅度)不变,通过调整δ来调整直流电压和无功,跟踪输出无功功率,直流电压就会大幅度随动,无功相应速度较慢。SVG通常有他励方式和自励方式,他励方式需要提供外部的直流电源,自励方式通过电网输出有功功率至SVG建立SVG的直流电压。δ为Us和Uc之间的相角差。

weixin_42668920的博客 1989

627A - XOR Equation 数学

题目:627A - XOR Equation Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)? Input The first line of the

01的世界 689

Python+gensim【中文LDA】简洁模型

文章目录完整代码过程详解words_lsdictionarydictionary.doc2bow函数corpuslda 完整代码 from gensim import corpora, models import jieba.posseg as jp # 待分析文本集 text1 = '美国教练坦言,没输给中国女排,是输给了郎平' text2 = '中国女排世界排名第一?真实水平如何,听听巴西和美...

ArYe 2万+

python模拟银行交易流水

模拟自动生成银行交易流水,其中包括:入账账号、出账账号、交易金额、交易余额、交易时间、出入表示关键字段

qq_31823889的博客 2910

371. Sum of Two Integers [easy] (Python)

题目链接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, retu

Coder_Orz的博客 7049

配置 SAP Fiori 服务的 ICF 节点:从 SICF 激活到访问链路打通的完整实践

在SAP Fiori项目实施中,ICF节点配置是关键前置条件却常被忽视。ICF(Internet Communication Framework)作为HTTP请求入口,需要通过SICF事务码激活相关服务节点,否则即使应用开发完善,用户仍可能遇到页面无法访问等问题。典型问题表现为:磁贴可点击但页面空白、资源加载失败等。激活路径主要涉及三组节点:bsp分支下的管理入口、ui5_ui5分支下的应用资源,以及public/bc下的公共资源。项目实施中需特别注意同名服务在不同分支下的差异化作用,确保完整链路通畅。IC

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 264

371. Sum of Two Integers(python+cpp)

题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example 1: Input: a = 1, b = 2 Output: 3 Example 2: Input: a = -2, b = 3 Output: 1 解释: 不适用加减法实...

小湉湉的博客 587

机器翻译的评价标准:BLEU、ROUGE等指标说明及代码实现

机器翻译领域常用的自动评价指标主要包括BLEU、ROUGE、METEOR和COMET四大类。BLEU基于n-gram精确率和短句惩罚,ROUGE关注召回率,METEOR结合精确率和召回率并考虑同义词,COMET则利用预训练模型计算语义相似度。这些指标各有特点:BLEU适用于机器翻译,ROUGE对长句友好,METEOR更接近人工评分,COMET无需参考译文。代码实现上,sacrebleu、rouge-score等库提供了便捷的计算方式。新兴趋势包括基于BERT等模型的语义评估和大语言模型评估器,但传统指标仍占

数据知道的博客 9418

把 MANDT 锁进保险箱:ABAP Client Handling 与 Client Isolation(聚焦 AMDP 的 Client Safety )

本文探讨了ABAP系统中Client Handling与Client Isolation机制的演进,重点分析了AMDP环境下多租户隔离的风险与解决方案。传统ABAP SQL/CDS通过隐式Client过滤提供安全保障,而AMDP因其原生SQLScript特性容易绕过隔离机制。文章对比了旧时代依赖开发自觉的传参方式与新时代通过AMDP OPTIONS强约束的语法方案,详细介绍了CLIENT INDEPENDENT和CDS SESSION CLIENT DEPENDENT两种编译期安全机制,阐述了USING列表

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 341

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

【LeetCode】371. Sum of Two Integers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum-of-two-integers/description/ 题目描述: Calculate the sum of two integers a and b, but you ...

负雪明烛 2859

ESP32-C3入门教程 蓝牙篇①——基于Blufi 的 WiFi 智能配网

文章目录一、前言二、WiFi配网方式三、快速运行四、运行效果五、ESP32-C3程序流程5.1 主程序5.2 回调函数5.3 SmartConfig线程5.4 流程图六、关键函数6.1 设置SmartConfig的协议类型6.2 启动SmartConfig6.3 停止SmartConfig七、其他八、参考 一、前言 本文基于VS Code IDE进行编程、编译、下载、运行等操作 基础知识点请查阅:ESP32-C3入门教程 基础篇①——基于VS Code构建Hello World 二、WiFi配网方式 W

7765

LeetCode-371-Sum of Two Integers-E

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. 解决方案: int getSum(int a, int b) { if(b!=0) r

文卿的藏剑山庄 135

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode-Bit Manipulation-371-E:两整数之和(Sum of Two Integers)

文章目录思路解法 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 思路 (1)位运算详解以及在 Python 中需要的特殊处理-江不知 a + b 的问题拆分为 (a 和 b 的无进位结果) + (a 和 b 的进位结果) (1)无进位加法使用异或运算计算得出 (2)进位结果使用与运算和移位运算计算得出 (3)循环此过程,直到进位为 0 解法...

Heorin X的博客 162

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LeetCode //C - 371. Sum of Two Integers

【代码】LeetCode //C - 371. Sum of Two Integers。

Made in Code 953

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode笔记:371. Sum of Two Integers

不使用加号减号达到计算两个整数之和的目的

Minecraft 1083

leetcode-371-Sum of Two Integers

问题题目:[leetcode-371]思路 计算不带进位加法( a ^ b ) 计算进位( a & b ) 累加这两部分的和(???) 思路大致如上面的步骤,问题是本省需要实现加法,但是第三步又要用到加法,所以第三步的实现还是通过前两部分。直到没有进位,那么此时的加法退化成不带进位加法,用xor实现即可。代码class Solution { public: int getSum(int a,

Kang_TJU的博客 509

LeetCode 371. Sum of Two Integers

题目: 计算两个整数a和b的和,但是不允许使用+和-运算符。 Calculate the sum of two integersaandb, but you arenot allowedto use the operator+and-. Input: a = 1, b = 2 Output: 3 Input: a = -2, b = 3 Output: 1 ...

冲鸭!!! 233

leetcode 371. Sum of Two Integers 按位操作 python3

一.问题描述 Calculate the sum of two integersaandb, but you arenot allowedto use the operator+and-. Example 1: Input: a = 1, b = 2 Output: 3 Example 2: Input: a = -2, b = 3 Output: 1 二.解题思...

CSerwangjun的博客 354

Algorithms practice:leetcode 371 Sum of Two Integers

【代码】Algorithms practice:leetcode 371 Sum of Two Integers。

fuyouzhiyi的博客 895

LeetCode 371:Sum of Two Integers

LeetCode 371:Sum of Two IntegersCalculate the sum of two integers a and b, but you are not allowed to use the operator + and -加法(a + b)用^、&和<<即可实现,a^b可得到对应位没有进位时的和,a&b可得到各位产生的进位值。如:a=010010, b=100111,计

BD的笔记 435

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. 这道题就让我们看看大神们的方法吧! public int getSum(int a, int b)

淡然坊 329

LeetCode 2.Add Two Numbers,67.Add Binary,371.Sum of Two Integers,66.Plus One

LeetCode 2.Add Two Numbers, LeetCode 67.Add Binary, LeetCode 371.Sum of Two Integers, LeetCode 66.Plus One

My Blog 863

Leetcode PHP题解--D84 371. Sum of Two Integers

D84 371. Sum of Two Integers 题目链接 371. Sum of Two Integers 题目分析 相加给定的两个数,但不能使用+或-运算符。 思路 可以用二进制的与运算完成。此处用array_sum完成。 最终代码 <?php class Solution ...

138

leetCode刷题记录48_371_Sum of Two Integers

/*****************************************************问题描述************************************************* Calculate the sum of two integers a and b, but you are not allowed to use the operator + and...

gunsmoke的专栏 227
上一篇: 【leetcode】368. Largest Divisible Subset【M】【65】
下一篇: 【leetcode】367. Valid Perfect Square【M】【35】
Vicky_Vicky_990121
博客等级 码龄14年 23粉丝 187原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值