当前位置:网站首页>LeetCode 剑指 Offer 65. 不用加减乘除做加法***
LeetCode 剑指 Offer 65. 不用加减乘除做加法***
2022-04-22 04:38:00 【暮雨林钟】
具体思想:
这里采用的思路注意两点:
1.其实相当于算完进位左移后,再次当作加数和非进位数相加,直到进位为0为止;
2.移位是注意一下,c++要求有符号负数不能随意左移,是未定义行为,因此要把该数转化为无符号数进行左移;
值得注意的是,符号在进位计算内仅作修饰,也就是正数和负数可以直接按照相同进行计算,不用太管符号位;
具体代码:
class Solution {
public:
int add(int a, int b) {
while(b!=0){
int c=(a&b);
a^=b;
b=unsigned(c)<<1;
}
return a;
}
};
版权声明
本文为[暮雨林钟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_66776925/article/details/124334989
边栏推荐
- 链表(Linked List)第一篇
- STL的unique函数返回值
- 二叉树的层序遍历
- How to combine acrobat Pro DC with other files to create a single PDF file?
- MUI-弹出菜单
- 同行面试分享 联想 winform方向 20220420
- Oracle password expiration problem, set never expire
- 15.bufferevent客户端测试连接服务器
- When the SQL statement is executed by hyperf, the parameter will have two single quotes
- Pod of kubernetes cluster said, can I die with dignity?
猜你喜欢

【S32K3_STM&PIT_MCAL】
![[twelfth database operation - stored procedure]](/img/8f/5ca161db5ffb465dddb9d423418fad.png)
[twelfth database operation - stored procedure]

Thread pool status + ThreadPoolExecutor
![[experience] Why does the IP address of HP printer start with 169.254](/img/e7/5068a565b57f066377fcb5754bc4bd.png)
[experience] Why does the IP address of HP printer start with 169.254

创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材

Bi set - SSM campus second-hand book sales system + thesis

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
![[concurrent programming 043] how to solve the problems of CAS and ABA?](/img/bd/8638aa75d7d3b237d792a201e68c0f.png)
[concurrent programming 043] how to solve the problems of CAS and ABA?

2022山东省安全员C证特种作业证考试题库及答案

同行面试分享 联想 winform方向 20220420
随机推荐
链表第三篇
爬演员名字加链接
2022山东省安全员C证特种作业证考试题库及答案
22.4.21学习感悟
排序一
STL的unique函数返回值
C language string
Redis 的过期数据会被立马删除么?
How much do you know about the testing methods of software testing?
Verify simple sorting using logarithm
Shell variables $, $@, $0, $1, $2, ${},%% use explanation and easy-to-use shell formatting tools
C entry - check the ID number with regular expressions.
Spark installation and use of educoder
pipeline communication
C语言学习记录——삼십칠 字符串函数使用和剖析(1)
Pgpool II 4.3 Chinese Manual - introductory tutorial
手机软件(App)测试主要有哪些方面?
Cilcate environment construction
crypto-js加密算法库【安装教程、缓存加密】
Torch file saving and loading -- [torch learning notes]