Leetcode 371: Sum of Two Integers(使用位运算实现)
题目是:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
思路:两个数的加法分为两步,对应位相加和进位。
举个简单的例子:997+24

我们平时计算时是将对应位相加和进位同时计算,其实可以保留下进位,只计算对应位相加,保留进位的位置(值)。接下来,将进位向左移动一位,将上一步的结果与移位后的进位值进行对应位相加,直到没有进位结束。
对于二进制数的而言,对应位相加就可以使用异或(xor)操作,计算进位就可以使用与(and)操作,在下一步进行对应位相加前,对进位数使用移位操作(<<)。
这样就非常好理解下面的实现代码。
int getSum(int a, int b)
{
while (b)
{
int c = a ^ b;
b = (a & b) << ;
a = c;
}
return a;
}
最后,再给一个详细的运行过程示意,计算523+1125.(另外,如果是有负数的话,算法也是可行的,可以去看一下补码的相关内容)

Leetcode 371: Sum of Two Integers(使用位运算实现)的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode: 371 Sum of Two Integers(easy)
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
随机推荐
- C# 基于委托的事件
事件基于多播委托的特性. 多播委托事实上就是一组类型安全的函数指针管理器,调用则执行顺序跳转到数组里所有的函数指针里执行. class Program { public class CarInfoEv ...
- codevs 1709 钉子和小球
1709 钉子和小球 1999年NOI全国竞赛 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果题目描述 Description有一个三角形木板 ...
- 让乔布斯立足肩上的C语言之父
2011年,人们对乔布斯的去世记忆深刻,但这一年还有另一位本应获得同样关注的人物也与世长辞,他就是C语言之父丹尼斯·里奇(Dennis Ritchie). 不过,并非所有人都没能正确认识到里奇所曾作出 ...
- C语言学习笔记--字符串函数
字符串函数 需要包含头文件#include<stdio.h> strlen strcmp strcpy strchr strstr strcasestr
- phonegap apk
在 Windows 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序本贴首发于:http://xuekaiyuan.com/forum.php?mod=vie ...
- 开发者应该避免使用的6个Java功能(转)
本文作者是一名拥有多年Java开发经验的程序员,他从经验中得出,并不是所有的Java SE功能/API都值得程序员去使用,比如本文列举的这6个,大家在使用前得慎重对待.以下是对原文的摘译. 多年的Ja ...
- -_-#【AJAX】XMLHttpRequest
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Linux下DVD-R刻录问题
之前CD的刻录一直使用的命令行工具集cdrtools中的mkisofs.cdrecord.然后本来刻录DVD可以使用它的growisofs命令. 现在假设原始文件目录为/src/,目标目录为/dest ...
- windows server 2003 64x 读取office数据终极解决办法 The 'Microsoft.Jet.OLEDB.4.0' provider is not registered
微软老子信了你的邪! 试了各种办法没有效果 网友解决办法一: The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the ...
- "蓝筹"如何使程序猿?
"蓝筹"这个词可能不是很多人知道这意味着什么.我会来普及知识.这是最重要的概念是指"越来越从长远来看更有价值"的含义.作为一个程序猿,我想你想使自己通过实际行动 ...