[leetcode]: 371. Sum of Two Integers
来源:互联网 发布:跟淘宝联盟类似的网站 编辑:程序博客网 时间:2024/06/14 20:40
1.题目描述
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
翻译:不使用+,-符号,计算两个整数的和
Example:
Given a = 1 and b = 2, return 3.
2.分析
首先想到电路里面加法器的实现:
保留位remain=a^b 0^1=1,1^0=1,0^0=0,1^1=0
进位carry=a&b<<1
3.代码
class Solution {public: int getSum(int a, int b) { int remain = a; int carry = b; while (carry) { int temp = carry^remain;//异或 carry = (carry&remain) << 1;//进位 remain = temp; } return remain; }};
0 0
- LeetCode 371. Sum of Two Integers
- 371. Sum of Two Integers LeetCode OJ
- [Leetcode]371. Sum of Two Integers
- LeetCode - 371. Sum of Two Integers
- <LeetCode OJ> 371. Sum of Two Integers
- LeetCode 371. Sum of Two Integers
- leetcode 371. Sum of Two Integers
- leetcode 371.Sum of Two Integers
- LeetCode 371. Sum of Two Integers
- 371. Sum of Two Integers(Leetcode)
- leetcode 371. Sum of Two Integers
- 【leetcode】371. Sum of Two Integers【E】
- [leetcode] 371. Sum of Two Integers
- leetcode.371. Sum of Two Integers
- 371. Sum of Two Integers--LeetCode Record
- LeetCode—371. Sum of Two Integers
- Leetcode 371. Sum of Two Integers
- LeetCode 371. Sum of Two Integers
- 易聚之外部设备
- 从零开始学贪心算法
- java继承简例
- tab栏切换(点击图片切换路径)
- 何为术
- [leetcode]: 371. Sum of Two Integers
- 汇编-循环编程
- 使用DOM4J方式生成XML文件的步骤如下:
- Android蓝牙事件
- 排序别名使用中文出错
- Hessian学习
- 生死门
- 5,jedis连接池
- Ios支付宝开发流程与常见的问题