当前位置:网站首页>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;
}
}
边栏推荐
- LOGO特訓營 第三節 首字母創意手法
- Logo special training camp section 1 Identification logo and logo design ideas
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Interview essential leetcode linked list algorithm question summary, whole process dry goods!
- How to send a reliable request before closing the page
- 环境加密技术解析
- UML图记忆技巧
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- leetcode 72. Edit distance edit distance (medium)
- 测试必会:BUG的分类及推进解决
猜你喜欢
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
攻防世界 misc 进阶区 2017_Dating_in_Singapore
LOGO特训营 第五节 字体结构与设计常用技法
【OpenGL】笔记二十九、抗锯齿(MSAA)
Concurrent optimization summary
Mongodb aggregation operation summary
sobel过滤器
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Attack and defense world misc advanced area ditf
LOGO特訓營 第三節 首字母創意手法
随机推荐
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
LOGO特训营 第一节 鉴别Logo与Logo设计思路
About stack area, heap area, global area, text constant area and program code area
UML diagram memory skills
MYSQL架构——逻辑架构
MySQL storage data encryption
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
虚拟人产业面临的挑战
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
Test will: bug classification and promotion solution
MySQL Architecture - logical architecture
LOGO特訓營 第三節 首字母創意手法
Solana chain application crema was shut down due to hacker attacks
MySQL Architecture - user rights and management
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
Shell 脚本实现应用服务日志入库 Mysql
The overview and definition of clusters can be seen at a glance
LOGO special training camp section I identification logo and Logo Design Ideas
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用