[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      * @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 };
时间: 2024-11-05 09:12:40

[LintCode] A + B 问题的相关文章

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

Lintcode 469. 等价二叉树

----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */

Lintcode 75.寻找峰值

--------------------------------------- 按照给定的峰值定义,峰值的左半部分一定是递增的,所以只要找到不递增的即可. AC代码: class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { for(int i=1;i<A.length;i++){ if(A[i]>=

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

Lintcode 97.二叉树的最大深度

--------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class S

[Lintcode two-sum]两数之和(python,双指针)

题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个指针分别从左从右开始扫描,每次判断这两个数相加是不是符合题意,如果小了,那就把左边的指针向右移,同理右指针.然后在备份的数组里找到位置. 1 class Solution: 2 """ 3 @param numbers : An array of Integer 4 @param

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [