当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Concurrent network modular reading notes transfer
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
- 繁华落尽、物是人非:个人站长该何去何从
- The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
- LOGO特训营 第四节 字体设计的重要性
- 关于栈区、堆区、全局区、文字常量区、程序代码区
- 堆排序代码详解
- 10 schemes to ensure interface data security
- leetcode 72. Edit Distance 编辑距离(中等)
猜你喜欢
PMO: compare the sample efficiency of 25 molecular optimization methods
MySQL Architecture - user rights and management
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
业务太忙,真的是没时间搞自动化理由吗?
Challenges faced by virtual human industry
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
攻防世界 MISC 进阶区 hit-the-core
达梦数据凭什么被称为国产数据库“第一股”?
It is said that software testing is very simple, but why are there so many dissuasions?
攻防世界 misc 进阶区 2017_Dating_in_Singapore
随机推荐
啃下大骨头——排序(二)
LOGO特训营 第四节 字体设计的重要性
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
Common open source codeless testing tools
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
不同环境相同配置项的内容如何diff差异?
Erik baleog and Olaf, advanced area of misc in the attack and defense world
环境加密技术解析
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
高中物理:直线运动
sobel过滤器
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Wake up day, how do I step by step towards the road of software testing
蓝队攻防演练中的三段作战
PostgreSQLSQL高级技巧透视表
UML diagram memory skills