当前位置:网站首页>剑指 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;
}
}
边栏推荐
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Detailed explanation of heap sort code
- Summary of index operations in mongodb
- Locust性能测试 —— 环境搭建及使用
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- Force buckle 2_ 1480. Dynamic sum of one-dimensional array
- 攻防世界 MISC 高手进阶区 001 normal_png
- Unity-VScode-Emmylua配置报错解决
- How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)
- 攻防世界 MISC 进阶区 3-11
猜你喜欢
LOGO特訓營 第三節 首字母創意手法
Mongodb aggregation operation summary
Domestic database chaos
MYSQL架构——逻辑架构
达梦数据凭什么被称为国产数据库“第一股”?
The overview and definition of clusters can be seen at a glance
UML图记忆技巧
攻防世界 MISC 进阶区 Ditf
Tiktok actual combat ~ the number of comments is updated synchronously
LOGO special training camp section I identification logo and Logo Design Ideas
随机推荐
Attack and defense world misc advanced area ditf
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
测试必会:BUG的分类及推进解决
LOGO特训营 第三节 首字母创意手法
面试必备 LeetCode 链表算法题汇总,全程干货!
Force buckle_ Palindrome number
PostgreSQL JOIN实践及原理
集群的概述与定义,一看就会
Introducing QA into the software development lifecycle is the best practice that engineers should follow
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
蓝队攻防演练中的三段作战
High school physics: linear motion
Logo special training camp section II collocation relationship between words and graphics
短视频系统源码,点击屏幕空白处键盘不自动收起
Flask 上下文详解
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
达梦数据凭什么被称为国产数据库“第一股”?
MySQL storage data encryption
LOGO特训营 第五节 字体结构与设计常用技法
Redis的持久化机制