当前位置:网站首页>Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
2022-07-04 22:45:00 【LuZhouShiLi】
The finger of the sword Offer 65. Do not add, subtract, multiply or divide
subject
Write a function , Find the sum of two integers , It is required that... Should not be used in the function body “+”、“-”、“*”、“/” Four operation symbols .
Ideas
Reference resources K God's solution :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/
- No carry sum is the same as XOR operation
- The law of carry and operation is the same ( Need to move one bit left )
Code
class Solution {
public int add(int a, int b) {
while(b != 0)
{
int c = (a & b) << 1;// c = carry
a ^= b;// Non carry and Exclusive or operation
b = c;// b = carry
}
return a;
}
}
边栏推荐
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- UML图记忆技巧
- LOGO特训营 第四节 字体设计的重要性
- MySQL Architecture - logical architecture
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- Postgresqlql advanced skills pivot table
- 微服务--开篇
- 攻防世界 MISC 进阶区 can_has_stdio?
- Easy to use app recommendation: scan QR code, scan barcode and view history
猜你喜欢
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
堆排序代码详解
串口数据帧
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
MYSQL架构——用户权限与管理
Serial port data frame
Redis的持久化机制
Attack and defense world misc advanced area ditf
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Why is Dameng data called the "first share" of domestic databases?
随机推荐
堆排序代码详解
LOGO特训营 第五节 字体结构与设计常用技法
Attack and defense world misc advanced area Hong
Solana chain application crema was shut down due to hacker attacks
sobel过滤器
Concurrent optimization summary
Test will: bug classification and promotion solution
攻防世界 MISC 进阶区 can_has_stdio?
串口数据帧
测试必会:BUG的分类及推进解决
How to send a reliable request before closing the page
不同环境相同配置项的内容如何diff差异?
Detailed explanation of flask context
SPSS installation and activation tutorial (including network disk link)
【烹饪记录】--- 青椒炒千张
Create Ca and issue certificate through go language
质量体系建设之路的分分合合
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
md5工具类