当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 攻防世界 MISC 进阶 glance-50
- SPSS安装激活教程(包含网盘链接)
- Logo special training camp section 1 Identification logo and logo design ideas
- PMO: compare the sample efficiency of 25 molecular optimization methods
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Scala download and configuration
- Logo special training camp Section V font structure and common design techniques
- 通过Go语言创建CA与签发证书
- How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)
- Unity-VScode-Emmylua配置报错解决
猜你喜欢
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
UML diagram memory skills
Persistence mechanism of redis
Mongodb aggregation operation summary
Logo special training camp section II collocation relationship between words and graphics
Scala download and configuration
Common open source codeless testing tools
Introduction and application of bigfilter global transaction anti duplication component
LOGO特训营 第二节 文字与图形的搭配关系
随机推荐
Shell script implements application service log warehousing MySQL
UML diagram memory skills
Logo special training camp Section V font structure and common design techniques
Wake up day, how do I step by step towards the road of software testing
【烹饪记录】--- 青椒炒千张
PostgreSQLSQL高级技巧透视表
SPSS installation and activation tutorial (including network disk link)
Sqlserver encrypts and decrypts data
Why is Dameng data called the "first share" of domestic databases?
Jvm-Sandbox-Repeater的部署
LOGO特訓營 第三節 首字母創意手法
通过Go语言创建CA与签发证书
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
MYSQL架构——用户权限与管理
Introduction and application of bigfilter global transaction anti duplication component
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
Tla+ introductory tutorial (1): introduction to formal methods
Summary of index operations in mongodb
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集