当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Attack and defense world misc advanced area Hong
- 微服务--开篇
- 【烹饪记录】--- 青椒炒千张
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
- php短视频源码,点赞时会有大拇指动画飘起
- Li Kou 98: verify binary search tree
- idea中pom.xml依赖无法导入
- 攻防世界 misc 高手进阶区 a_good_idea
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- 30余家机构联合发起数字藏品行业倡议,未来会如何前进?
猜你喜欢
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Redis的持久化机制
BigFilter全局交易防重组件的介绍与应用
Attack and defense world misc advanced area Hong
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
攻防世界 MISC 进阶区 Ditf
LOGO特训营 第三节 首字母创意手法
Business is too busy. Is there really no reason to have time for automation?
Challenges faced by virtual human industry
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
随机推荐
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Close system call analysis - Performance Optimization
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
攻防世界 MISC 进阶 glance-50
How to send a reliable request before closing the page
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
Common shortcut keys for hbuilder x
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
不同环境相同配置项的内容如何diff差异?
Postgresqlql advanced skills pivot table
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Force buckle_ Palindrome number
安装人大金仓数据库
环境加密技术解析
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
The proofreading activity of data science on the command line second edition was restarted
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
繁华落尽、物是人非:个人站长该何去何从
Jvm-Sandbox-Repeater的部署
Locust performance test - environment construction and use