[LintCode] A + B 问题

简介: Bit-by-Bit summation: 1 class Solution { 2 public: 3 /* 4 * @param a: The first integer 5 * @param b: The second integer 6...

Bit-by-Bit summation:

 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         int res = 0, sum = 0, carry = 0;
11         for (int i = 0; i < 32; i++, a >>= 1, b >>= 1){
12             int d1 = a & 1, d2 = b & 1;
13             sum = (d1 ^ d2 ^ carry);
14             carry = max((d1 & d2), max((d1 & carry), (d2 & carry)));
15             res ^= (sum << i);
16         }
17         return res;
18     }
19 };

Treat a + b as two parts:

  1. a + b without carry;
  2. carry of a + b;
  3. recursively plus part 1 and part 2 until no carry exists.
 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         while (b) {
11             int carry = a & b;  // carry
12             a ^= b;             // plus without carry
13             b = carry << 1;     // recursively plus the two parts
14         }
15         return a;
16     }
17 };

The code can be further shortened by writing it recursively.

 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         if (!b) return a;
11         return aplusb(a ^ b, (a & b) << 1);
12     }
13 };

Or just in one line :-) 

 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         return (!b ? a : aplusb(a ^ b, (a & b) << 1));
11     }
12 };

 

目录
相关文章
|
8月前
|
算法 Java C语言
C++和Java中的随机函数你玩明白了吗?内附LeetCode470.rand7()爆改rand10()巨详细题解,带你打败LeetCode%99选手
C++和Java中的随机函数你玩明白了吗?内附LeetCode470.rand7()爆改rand10()巨详细题解,带你打败LeetCode%99选手
[leetcode/lintcode 题解] 阿里算法面试题:猜数字游戏
[leetcode/lintcode 题解] 阿里算法面试题:猜数字游戏
[leetcode/lintcode 题解] 阿里算法面试题:猜数字游戏
[leetcode/lintcode 题解]算法面试高频题详解: 对称树
[leetcode/lintcode 题解]算法面试高频题详解: 对称树
[leetcode/lintcode 题解]算法面试高频题详解: 对称树
|
人工智能 Java vr&ar
[leetcode/lintcode 题解] 阿里巴巴面试题:金字塔
[leetcode/lintcode 题解] 阿里巴巴面试题:金字塔
[leetcode/lintcode 题解] 阿里巴巴面试题:金字塔
|
机器学习/深度学习 算法
[leetcode/lintcode 题解] 算法面试高频题详解: 主元素 III
[leetcode/lintcode 题解] 算法面试高频题详解: 主元素 III
[leetcode/lintcode 题解] 算法面试高频题详解: 主元素 III
[leetcode/lintcode 题解] 算法面试真题详解:范围模块
[leetcode/lintcode 题解] 算法面试真题详解:范围模块
[leetcode/lintcode 题解] 算法面试真题详解:范围模块
[leetcode/lintcode 题解] 算法面试高频题:加热器
[leetcode/lintcode 题解] 算法面试高频题:加热器
[leetcode/lintcode 题解] 算法面试高频题:加热器
|
自然语言处理 算法 搜索推荐
[leetcode/lintcode 题解]算法面试真题详解:外星人字典
[leetcode/lintcode 题解]算法面试真题详解:外星人字典
[leetcode/lintcode 题解]算法面试真题详解:外星人字典
|
机器学习/深度学习 存储 算法
LintCode 题解丨大厂面试题:N皇后问题
LintCode 题解丨大厂面试题:N皇后问题
LintCode 题解丨大厂面试题:N皇后问题

热门文章

最新文章

下一篇
开通oss服务