Leetcode-371-Sum of Two Intergers
来源:互联网 发布:笨驴软件 编辑:程序博客网 时间:2024/06/06 08:32
题目要求:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
求两个整型数的和,但是不可使用+和-运算符
例:Given a = 1 and b = 2, return 3.
解题思路:1.既然不能使用“+”“-”运算符,就考虑位运算
2.例如13+1,13(1101)1(0001),对两个数按位求异或,可得1100,易知:1和0求得为1,0和0求得为0,都没问题,问题在于1和1求得为0,应该是将其向左进1,所以解决的办法就是求得同为1的位,即求按位求与可得,并且左移以为,在将其加到结果中即可求得
代码如下:
<script type="text/javascript">/** * @param {number} a * @param {number} b * @return {number} */var getSum = function(a, b) { while(a != 0){ var remove = (a & b) << 1; b = a ^ b; a = remove; } alert(b);};getSum(11,1); </script>
0 0
- LeetCode 371 Sum of Two Intergers
- Leetcode-371-Sum of Two Intergers
- Leetcode Sum of Two Intergers
- LeetCode 371 Sum of Two Integers
- [leetcode-371]Sum of Two Integers(java)
- LeetCode[371] Sum of Two Integers
- LeetCode 371 : Sum of Two Integers(Java)
- leetcode NO.371 sum of two integers
- LeetCode#371 Sum of Two Integers
- leetcode 371 Sum of Two Integers C++
- LeetCode 371: Sum of Two Integers 题解
- LeetCode(371) Sum of Two Integers
- leetcode:bits:Sum of Two Integers(371)
- [LeetCode-371]Sum of Two Integers(C)
- LeetCode 371:Sum of Two Integers
- LeetCode 371 Sum of Two Integers
- Leetcode 371 Sum of Two Integers
- leetcode-371-Sum of Two Integers
- HDFS学习笔记
- Permission denied (publickey)怎么办
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at First.Demo1_13.main(Demo1
- Git命令使用手册
- Android--使用Intent打开各种文件
- Leetcode-371-Sum of Two Intergers
- spring中@value注解需要注意
- python编码错误:UnicodeDecodeError: 'utf8' codec can't decode
- RPC 分布式事务的疑问
- java中为什么要实现序列化,什么时候实现序列化?
- 第一篇博客-AspectJ的简单理
- c++对象的动态创建与释放
- TensorFlow官网教程Convolutional Neural Networks 难点详解
- [Leetcode] Majority Element