(Leetcode)371. Sum of Two Integers
来源:互联网 发布:java http文件服务器 编辑:程序博客网 时间:2024/04/29 02:59
371. Sum of Two Integers
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.
Solution
思路
题目要求不用+法进行加法运算,所以考虑用&和^运算来代替。
“^” XOR operation, 获得(a+b)的“和”in each bit
“&” AND operation, 获得(a+b)的“进位”in each bit.
因为进位是进到下一位的,所以要把(a&b)<<1
递归调用一下下
Code
public class Solution { public int getSum(int a, int b) { if(b==0) return a; int sum = a^b; int carrybit = (a&b)<<1; return getSum(sum,carrybit); }}
0 0
- (Leetcode)371. Sum of Two Integers
- 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
- hibernate—延迟加载
- java35期视频教程分享
- Java 泛型限定
- js 内置对象--array常用属性介绍
- Android为何比iOS卡?【转载】
- (Leetcode)371. Sum of Two Integers
- 对象转型 /java初学
- springmvc学习指南-springmvc的CRUD入门
- 数据结构:2-3树与红黑树
- Struts2工作原理
- 面向对象_static的注意事项
- [linux]jenkins迁移
- PHP内核之Apache的PHP模块启动
- 【Codeforces Round #351 Div. 2】 673A Bear and Game