当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Domestic database chaos
- [cooking record] - stir fried 1000 pieces of green pepper
- Locust性能测试 —— 环境搭建及使用
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- 页面关闭前,如何发送一个可靠请求
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- Logo special training camp section 1 Identification logo and logo design ideas
- LOGO special training camp section I identification logo and Logo Design Ideas
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
- Logo special training camp Section V font structure and common design techniques
猜你喜欢
随机推荐
10 schemes to ensure interface data security
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
LOGO特训营 第五节 字体结构与设计常用技法
MYSQL架构——用户权限与管理
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
Microservices -- Opening
leetcode 72. Edit distance edit distance (medium)
Logo special training camp section 1 Identification logo and logo design ideas
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
页面关闭前,如何发送一个可靠请求
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Introduction and application of bigfilter global transaction anti duplication component
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
PHP short video source code, thumb animation will float when you like it
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
短视频系统源码,点击屏幕空白处键盘不自动收起
Close system call analysis - Performance Optimization
【烹饪记录】--- 青椒炒千张
How to send a reliable request before closing the page
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展








