当前位置:网站首页>剑指 Offer 65. 不用加减乘除做加法
剑指 Offer 65. 不用加减乘除做加法
2022-07-04 22:20:00 【LuZhouShiLi】
剑指 Offer 65. 不用加减乘除做加法
题目
写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。
思路
参考K神的题解:https://leetcode.cn/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/solution/mian-shi-ti-65-bu-yong-jia-jian-cheng-chu-zuo-ji-7/
- 无进位和与异或运算规律相同
- 进位和与运算规律相同(需要左移一位)
代码
class Solution {
public int add(int a, int b) {
while(b != 0)
{
int c = (a & b) << 1;// c = 进位
a ^= b;// 非进位和 异或运算
b = c;// b = 进位
}
return a;
}
}
边栏推荐
- Logo special training camp Section IV importance of font design
- How to reset the password of MySQL root account
- UML图记忆技巧
- LOGO special training camp section I identification logo and Logo Design Ideas
- 集群的概述与定义,一看就会
- The overview and definition of clusters can be seen at a glance
- MYSQL架构——逻辑架构
- Attack and defense world misc advanced area Hong
- Force buckle 2_ 1480. Dynamic sum of one-dimensional array
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
猜你喜欢

【OpenGL】笔记二十九、抗锯齿(MSAA)

攻防世界 MISC 进阶区 3-11

Close system call analysis - Performance Optimization

Breakpoint debugging under vs2019 c release

Li Kou 98: verify binary search tree

Locust性能测试 —— 环境搭建及使用

Attack and defense world misc advanced area Hong

攻防世界 MISC 进阶 glance-50

Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像

醒悟的日子,我是怎么一步一步走向软件测试的道路
随机推荐
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
Domestic database chaos
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
PostgreSQLSQL高级技巧透视表
Challenges faced by virtual human industry
蓝队攻防演练中的三段作战
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
攻防世界 misc 高手进阶区 a_good_idea
Common shortcut keys for hbuilder x
LOGO特训营 第四节 字体设计的重要性
Logo special training camp section 1 Identification logo and logo design ideas
How to send a reliable request before closing the page
模拟摇杆控制舵机
环境加密技术解析
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
Breakpoint debugging under vs2019 c release
How to reset the password of MySQL root account
Mysql root 账号如何重置密码
leetcode 72. Edit Distance 编辑距离(中等)