码迷,mamicode.com
首页 > 其他好文 > 详细

A + B Problem

时间:2016-07-06 09:51:45      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

Write a function that add two numbers A and B. You should not use + or any arithmetic operators.

分析:

典型的Bit Operation.

 1 class Solution {
 2     /*
 3      * param a: The first integer
 4      * param b: The second integer
 5      * return: The sum of a and b
 6      */
 7     public int aplusb(int a, int b) {
 8         if (a == 0) return b;
 9         int sum = a ^ b;
10         int carry = (a & b) << 1;
11         return aplusb(carry, sum);
12     }
13 };

 

A + B Problem

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5645569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!