371. Sum of Two Integers也许是最简洁易懂的做法了
2016-07-03 17:25
513 查看
Calculate the sum of two integers a and b, but you are not allowed to use the operator
Example:
Given a = 1 and b = 2, return 3.
实现:
class Solution {
public:
int getSum(int a, int b) {
while (b){
int x = a ^ b, y = (a & b) << 1;
a = x; b = y;
}
return a;
}
};
+and
-.
Example:
Given a = 1 and b = 2, return 3.
实现:
class Solution {
public:
int getSum(int a, int b) {
while (b){
int x = a ^ b, y = (a & b) << 1;
a = x; b = y;
}
return a;
}
};
相关文章推荐
- mysql中sum float类型使用小数点的方法
- Mysql中的count()与sum()区别详细介绍
- MySQL中的SUM函数使用教程
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- 【python】编程学习练习题-2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode