Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example 1:Input: a = 1, b = 2
Output: 3
Example 2:Input: a = -2, b = 3
Output: 1

指定两个整数,对齐求和,但是不可以使用加号或者减号。

这个题16年校招的时候遇到过,所以有点印象,解法就是按照位运算来求解,我们可以看一下位运算,分析一下:
1.将两个整数以二进制表示,两个数的加法,即对应位上的数加法,如果两个数的某一位不同(一个为0,一个为1),则相加后该位为1;如果相同,分两种情况,同为0,则相加后还是0,如果同为1,相加后结果仍为0,但需要向更高位进位;
2.对位进行相加时,如果两个位相同,结果为0,不同,结果为1,这即位的异或运算;进位仅发生在两个位同为1的情况下,即位的与运算,与运算的结果,位为1的表示该位上发生了进位,进位实质上就是在更高位上加1,即左移一位,于是,最终的结果为异或的结果与进位的结果之和,问题又回到了求两个数之和,重复上述操作,直到进位为0为止;复杂度为O(n)

class Solution {public int getSum(int a, int b) {int bitXor = 0;int bitAnd = 0;while (0 != b) {bitXor = a ^ b;  // 异或,提取出不同位bitAnd = a & b;  // 与,提取进位a = bitXor;b = bitAnd << 1;}return a;}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum of Two Integers.
Memory Usage: 31.6 MB, less than 100.00% of Java online submissions for Sum of Two Integers.

下面是discuss中的解法,有的类似有的利用了递归形式。

code1和我的思路基本相同,
首先对a和b进行按位与,求一个结果carry,
然后对a和b进行按位异或,求一个结果并赋值给a,
最后最b赋值把carry的值左移一位。
一直循环,知道b为0或者这里不存在carry

class Solution {public int getSum(int a, int b) {if (a == 0) return b;if (b == 0) return a;while (b != 0) {int carry = a & b;a = a ^ b;b = carry << 1;}return a;}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum of Two Integers.
Memory Usage: 31.8 MB, less than 100.00% of Java online submissions for Sum of Two Integers.

下面是递归形式:

class Solution {public int getSum(int a, int b) {return (b == 0) ? a : getSum(a ^ b, (a & b) << 1);}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum of Two Integers.
Memory Usage: 31.7 MB, less than 100.00% of Java online submissions for Sum of Two Integers.

拓展:ab的减法:

public static int getSubtract(int a, int b) {while (b != 0) {int borrow = (~a) & b;a = a ^ b;b = borrow << 1;}return a;}

递归形式:

public static int getSubtract(int a, int b) {return (b == 0) ? a : getSubtract(a ^ b, (~a & b) << 1);
}

参考文章:
https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

https://en.wikipedia.org/wiki/Two%27s_complement

leetcode17 Sum of Two Integers相关推荐

  1. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  2. 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 ...

  3. sum of two integers

    https://leetcode.com/problems/sum-of-two-integers/ Calculate the sum of two integers a and b, but yo ...

  4. LeetCode之Sum of Two Integers

    1.题目 Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  5. Leetcode PHP题解--D84 371. Sum of Two Integers

    D84 371. Sum of Two Integers 题目链接 371. Sum of Two Integers 题目分析 相加给定的两个数,但不能使用+或-运算符. 思路 可以用二进制的与运算完 ...

  6. C#LeetCode刷题之#371-两整数之和(Sum of Two Integers)

    问题 不使用运算符 + 和 - ,计算两整数 a .b 之和. 输入: a = 1, b = 2 输出: 3 输入: a = -2, b = 3 输出: 1 Calculate the sum of ...

  7. AOJ0008 Sum of 4 Integers【暴力】

    Sum of 4 Integers Aizu - 0008 Write a program which reads an integer n and identifies the number of ...

  8. leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...

  9. LeetCode371——Sum of Two Integers(不用+)

    class Solution { public:int getSum(int a, int b) {int sum = a;while (b != 0){sum = a ^ b;//calculate ...

最新文章

  1. 汕头金山中学2021高考成绩查询,2019汕头市金山中学录取分数线及2019高考成绩喜报...
  2. Android 开发学习随笔
  3. Oracle日期函数和循环总结
  4. 核心组件:IRule
  5. webpack之externals操作三部曲--正确的姿势
  6. 电脑报合订本_电脑报2018年合订本
  7. Cainteoir Text-to-Speech 0.8 发布
  8. 如何解决飞秋FeiQ绑定端口错误
  9. Magento的主题Shaeng为网上时装店,配件商店,鞋专卖店创造
  10. 宏基E5572g 键盘排线插拔 拆机局部
  11. Python免费下载哔哩哔哩视频,只需一行代码!
  12. 写论文时如何翻译外文文献?
  13. python爬取豆瓣书籍_Python利用lxml模块爬取豆瓣读书排行榜的方法与分析
  14. MVC过滤器使用实例
  15. 楚汉骄雄之楚汉争雄 - 分集剧情介绍
  16. 会声会影2022版本软件下载安装使用激活教程
  17. iOS中使用基于RSA使用公钥加密和公钥解密
  18. 2022年高压电工考试题及高压电工作业考试题库
  19. 计算机屏保密码失效,电脑锁屏密码突然不对了是怎么回事
  20. 网站上的SSL是什么意思?有什么作用?

热门文章

  1. Linux-基本使用
  2. 黑客能追回被骗的钱吗
  3. 百度移动优化:关于移动端点击图片放大有多少人注意?
  4. python气象绘图技巧之箱线图
  5. 概说SEO中的过度优化
  6. ubuntu16.04 更新清华镜像源详细操作步骤
  7. 前端开发和html5,Web前端和HTML5前端相同吗 有区别吗
  8. 基于java爱宠医院管理系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  9. 一起来学k8s 38. 二进制k8s集群安装EFK
  10. Verilog中parameter使用