A + B Problem

来源:互联网 发布:ubuntu服务器重装系统 编辑:程序博客网 时间:2024/05/22 11:33

For given numbers a and b in function aplusb, return the sum of them.

注意

You don't need to parse the input and output. Just calculate and return.

样例

If a=1 and b=2 return 3

挑战

Can you do it with out + operation?

说明

Are a and b both 32-bit integers?

    - Yes.


class Solution {    /*     * param a: The first integer     * param b: The second integer     * return: The sum of a and b     */    public int aplusb(int a, int b) {       while ((a & b) > 0) {if (a < 0)a--;if (b < 0)b--;int x = (a ^ b);int y = (a & b) << 1;a = x < 0 ? x + 1 : x;b = y < 0 ? y + 1 : y;}return a | b;    }};


0 0
原创粉丝点击