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 ...
- C++ 常用的字符串处理函数实现
以下是一些标准库没有实现的函数,我觉得很方便就写了,估计会不定时更新. //根据一个文件的路径获取文件名 std::string file_name(const std::string& pa ...
- pl/sql 程序块里打印问题
declare v_number NUMBER; v_number2 NUMBER; begin select count(*) into v_number from dual; DBMS_OUTPU ...
- BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 606 Solved: 289 ...
- jstack(Stack Trace for Java)
功能 用于生成虚拟机当前时刻的线程快照(一般称为threaddump或javacore文件).线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间 ...
- where T:class 泛型类型约束
对于一个定义泛型类型为参数的函数,如果调用时传入的对象为T对象或者为T的子类,在函数体内部如果需要使用T的属性的方法时,我们可以给这个泛型增加约束: 类的定义 public class Product ...
- linux常用命令2
1.top 最近自己最常用的是 top d -1(每秒刷新一次) 主要看Mem used使用内存:CPU idle 剩余CPU:CPMMAND进程:以及%CPU进程所占用CPU. 目前主要是系统出问题 ...
- Visual Studio创建跨平台移动应用_02.Cordova Extension
1简介 本章节是关于Visual Studio Tools for Apache Cordova的,目前此产品只发布了预览版.Visual Studio for Apache Cordova帮助熟悉V ...
- 用Visual Studio创建集成了gtest的命令行工程
gtest代码库中的sample代码 在gtest的代码库中,包含了10个sample的代码,覆盖了gtest的常见用法,sample的代码位于以下文件夹: gtest\samples 由于gtest ...
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...