当前位置:网站首页>Leetcode refers to offer 65 No addition, subtraction, multiplication and division***
Leetcode refers to offer 65 No addition, subtraction, multiplication and division***
2022-04-22 04:41:00 【Twilight rain forest bell】
Specific ideas :
The idea adopted here should pay attention to two points :
1. In fact, it is equivalent to moving the carry left , Add again as an addend and a non carry number , Until the carry is 0 until ;
2. I want you to pay attention ,c++ Signed negative numbers are required and cannot be moved to the left at will , It's undefined behavior , Therefore, the number should be converted into an unsigned number and shifted to the left ;
It is worth noting that , The symbol is only used for decoration in the carry calculation , That is, positive and negative numbers can be calculated directly according to the same , Don't worry about the sign bit ;
Specific code :
class Solution {
public:
int add(int a, int b) {
while(b!=0){
int c=(a&b);
a^=b;
b=unsigned(c)<<1;
}
return a;
}
};
版权声明
本文为[Twilight rain forest bell]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220438342692.html
边栏推荐
- 链表第四篇
- 使用指定显卡
- 10. Libevent receives and processes server messages
- 论文阅读 (49):Big Data Security and Privacy Protection (科普文)
- [concurrent programming 046] for the synchronization method, how does the processor realize atomic operation?
- [concurrent programming 043] how to solve the problems of CAS and ABA?
- 14.buffferevent超时事件处理
- DHCP protocol workflow
- LeetCode 剑指 Offer 17. 打印从1到最大的n位数
- 22.4.21 learning comprehension
猜你喜欢

Kaggle actual combat 4.1 -- time series prediction problem

同行面试分享 联想 winform方向 20220420

Statistics of authoritative institutions: the best data center network company in 2021, China Huawei and H3C, were listed

10人小团队创业,如今收入过亿,不融资的简道云是怎么做到的?

Queue II

CommDGI: Community detection oriented deep graph infomax 2020 CIKM

2022 a special equipment related management (elevator) retraining question bank and answers

2022p cylinder filling test question bank and simulation test

【S32K3_STM&PIT_MCAL】
![[concurrent programming 045] what is pseudo shared memory sequence conflict? How to avoid?](/img/27/c0cc4a6ae809fc662277dfe5629b01.png)
[concurrent programming 045] what is pseudo shared memory sequence conflict? How to avoid?
随机推荐
Cilcate environment construction
[vi] summary
【S32K3_STM&PIT_MCAL】
软件测试成行业“薪”贵?
爬演员名字加链接
Crypto JS encryption algorithm library [installation tutorial, cache encryption]
crypto-js加密算法库【安装教程、缓存加密】
线程池状态+ThreadPoolExecutor
Cisco packet tracker experiment set
[untitled]
10人小团队创业,如今收入过亿,不融资的简道云是怎么做到的?
goland汉化解决方法(下载插件失败的情况下)
Thread pool status + ThreadPoolExecutor
How much do you know about the testing methods of software testing?
Random number of unity
Revit data processing (III)
7-2 symbol pairing | PTA
使用指定显卡
一文告诉你分析即服务(AaaS)到底是什么
Deployment of web server, personal experience