LeetCode 371. Sum of Two Integers
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 -
.
Example:
Given a = 1 and b = 2, return 3.
题目要求:计算两个整型的和,但是不能用+和-
我们知道a+b为((a&b)<<1)+(a^b),因此可以用递归的方法
class Solution { public: int getSum(int a, int b) { if (a == 0)return b; if (b == 0)return a; return getSum((a&b)<<1,a^b); } };
以上是关于LeetCode 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