[LeetCode] Sum of Two Integers 两数之和

单片机NTC热敏电阻测温:六种实用电路方案与选型指南 本文详细介绍了单片机系统中NTC热敏电阻测温的六种实用电路方案,包括分压法、恒流源法、RC充放电法、电桥法、频率法及集成专用IC方案,并提供了关键的硬件选型指南和软件处理技巧,帮助工程师实现高精度、低成本的温度测量。 阅读详情

 

 

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的新题都是到处抄来的么,讲解可以参见我之前的博客18.1 Add Two Numbers。简而言之就是用异或算不带进位的和,用与并左移1位来算进位,然后把两者加起来即可,先来看递归的写法如下:

 

解法一:

复制代码

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

复制代码

 

上面的解法可以精简到一行,哈哈,叼不叼?

 

解法二:

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

 

也可以写成迭代的样子,思路都是一样的~

 

解法三:

复制代码

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

复制代码

 

参考资料:

http://www.cnblogs.com/grandyang/p/5451942.html

https://leetcode.com/discuss/111682/one-line-java-code

https://leetcode.com/discuss/111601/6-line-java-solution

 

LeetCode All in One 题目讲解汇总(持续更新中...)

分类: LeetCode

SO_KEEPALIVE、TCP_KEEPIDLE、TCP_KEEPINTVL、保活包 是一个套接字选项,用于设置是否启用 keepalive 机制。在这段代码中没有涉及到选项的设置。当被设置为非零值时,表示启用 keepalive 机制。keepalive 是一种用于检测连接是否仍然有效的机制。通过定期发送一些特定的探测数据,可以检测到网络连接的异常中断或对端应用程序的崩溃退出。为了避免以上情况,可以启用 keepalive 机制,即使在无数据传输的情况下也定期发送探测数据。如果在一定时间内没有收到对端的响应,就认为连接已经失效,并进行相应的处理。要启用选项,需要使用函数。其中,fd。 阅读详情

相关推荐

Vivado新手避坑指南:搞懂Generate Output Products和Create HDL Wrapper到底在干啥

本文深入解析Vivado设计流程中的Generate Output Products和Create HDL Wrapper两大关键步骤,帮助新手理解其核心功能与操作要点。通过详细讲解文件生成类型、关键选项设置及常见问题解决方案,指导开发者正确使用这两个功能,避免设计流程中的常见错误,提高FPGA开发效率。

weixin_29207149的博客 313

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 小屋 764

不用SE63逐个翻译!ABAP函数LXE_OBJ_TEXT_PAIR_WRITE批量处理TCODE描述的3种姿势

本文介绍了三种利用ABAP函数LXE_OBJ_TEXT_PAIR_WRITE批量翻译SAP事务码(TCODE)描述的高效方案,旨在替代SE63逐个翻译的低效操作。内容涵盖从基础循环、Excel导入到全自动程序集成的实现方法,帮助开发者显著提升批量翻译效率,并深入解析了SAP翻译体系的核心原理与实战技巧。

weixin_42593130的博客 660

Sum of Two Integers

位运算、异或计算两个数不含进位的加和、与操作计算两个数字加和的进位、不使用运算符+和-实现a和b的加和。

不积跬步无以至千里 466

人工智能之语言领域 自然语言处理 第六章 情感分析

情感分析(Sentiment Analysis),又称意见挖掘(Opinion Mining),旨在自动识别和提取文本中表达的主观情感、态度或情绪。从用户评论到社交媒体,从产品反馈到政治舆情,情感分析已成为企业决策、品牌监控和人机交互的关键技术。本章将深入介绍情感分析的层级结构、核心技术、主流模型,并通过社交媒体舆情实战案例完整演示分析与可视化流程。层级目标示例词级(Word-level)判断单个词的情感倾向“好” → 正面;“差” → 负面句级(Sentence-level)判断整个句子的情感极性。

咚咚王者的博客 858

Paypal开发者中心获取“ClientId”和“ClientSecret”参数

正式环境设置 1、登录到 paypal开发者中心 网址:https://developer.paypal.com/developer/ 点击右上角登录按钮登录 2、左侧栏 - 我的应用程序和凭证 - 点击“创建新应用” 新注册的paypal账号,需要进行邮箱验证,验证通过后,需要等待一段时间(或重新登录),才会出现相关设置项位置 验证邮箱通过后,点击创建新应用 3、按提示填写,保存创建应用 4、点击刚创建的应用进入应用详情页 5、应用详情页获得网站开发人员所需要的“ClientId”和“Client

龚清林的博客 1万+

C. Two Arrays(DIV2)

You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that: the length of both arrays is equal to m; each element of each array is an integer between 1 and n (inclusive); ai≤bi for any index i from 1 to m; array a is sorted

s1050712899的博客 413

人工智能——框架表示法

1975年美国明斯基在“A framework for representing knowledge”提出了框架理论。 1. 框架及框架的一般表示形式 框架:一种描述对象属性的数据结构。 框架的一般表示形式 <框架名> 槽名1: 侧面名1:值1,值2…,值p1 侧面名2:值1,值2…,值p2 侧面名2:值1,值2…,值pm1 槽名2: 侧面名...

weixin_33759269的博客 1642

关于Leetcode中sum of integers一题的理解

关于Leetcode中sum of integers一题的理解及思路

zsy112371的专栏 1008

高频注入是否需要对齐和强拖操作?

高频注入技术显著优化了传统FOC启动流程,但仍需部分对齐操作。传统启动必须包含位置检测、对齐和强拖等步骤,而高频注入可实现零速闭环,简化或跳过部分环节。高频注入下的对齐主要用于验证有效性、初始化观测器和建立初始磁链,形式可大幅简化。虽然高频注入基本消除了传统强拖需求,但在重载、弱凸极等特殊情况下仍需辅助。实际应用中可采用自适应混合策略,根据负载和电机类型选择最优启动方案。高频注入使对齐和强拖从"必须"变为"可选优化项",是无传感器控制的重要进步。

汽车软件开发 209

x2-DFD:A framework for explainable and explainable deepfake detection

该文章提出了X2-DFD框架,用于可解释和可扩展的深度伪造检测。通过评估大型多模态语言模型(MLLMs)在深度伪造检测中的内在能力,发现它们在不同伪造相关特征上的有效性各异。基于此,实施了针对性的微调策略,显著提高了MLLMs的解释能力,并整合了外部深度伪造检测器(EDDs),以结合MLLMs和传统检测器的互补优势,实现更好的检测和解释。

志在山顶的人,不会贪念山腰的风景 512

LeetCode 371. Sum of Two Integers (两数之和)

Calculate the sum of two integersaandb, but you arenot allowedto use the operator+and-. Example:Givena= 1 andb= 2, return 3. 题目标签:Bit Manipulation   这道题目让我们做两数之和,当然包括负数,而且不能用+,-等符号...

weixin_30396699的博客 105

ResNet改进(7):添加 SK注意力机制

实现 SK 注意力机制。

2401_82355416的博客 732

【leetcode74】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: ...

144

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode371 - Sum of Two Integers - 利用位运算求两数之和 - 扩展: 不使用新的变量,交换两个变量的值。...

1. 利用位运算求两数之和 class Solution { public: int getSum(int a, int b) { int sum = a^b;// sum 是不带进位的的和 int carry = (a&b)<<1; //carry是进位的值 while(carry != 0) ...

dianjige2532的博客 74

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Lettcode | Two Sum(两个数的和等于定值)

题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to th

solar一抹阳光的专栏 1514

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【leetcode】371. 两整数之和( Sum of Two Integers )

题目描述 【leetcode】371. 两整数之和( Sum of Two Integers ) 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: a = -2, b = 3 输出: 1 第一次解答 思路 看了别人的题解 用异或进行相加 用按位与判断是否有进位 对于...

Liber-coder的博客 313

1. (Two Sum)两数之和

题目 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answ

OIqng的博客 5488

Leetcode日练笔记11 #350 #167 Intersection of Two Arrays II & Two Sum II - Input array is sorted

#350 Intersection of Two Arrays II Given two integer arraysnums1andnums2, returnan array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result inany order. 解题思路: 还是用回之...

Bittersweet_t的博客 1743

[leetcode] 371. 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

小榕流光的专栏 1万+

力扣1. Two Sum 两数之和 (python)

Leetcode 1. Two Sum 两数之和 (python) 题目 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same ele

rickyoung0221的博客 372

leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算。 第一次提交的时候,没想到输入包含负数,于是又调了好久。 既然题目是二进制运算,就借此机会复习一下补码吧。 需要知道: 正数的补码 = 其本身 负数的补码 = 源码取反 + 1 补码的运算如下,参考:补码加减法运算 class Solution { public int getSum(int a, int b)

寒泉 5万+

LeetCode //C - 371. Sum of Two Integers

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

Made in Code 953

Leetcode | Two Sum (两数之和)

Description: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can retu

shawnchang777的博客 253

LeetCode:371. Sum of Two Integers Sum of Two Integers两整数之和(C语言)

题目描述: 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: a = -2, b = 3 输出: 1 通过次数41,261 提交次数72,634 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/sum-of-two-integers 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解答: int getS

C/C++攻城狮 255
上一篇: 【LeetCode】Number Complement 解题报告
下一篇: Leetcode算法题-解法转载
binbigdata
博客等级 码龄9年 227粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值