本文共 423 字,大约阅读时间需要 1 分钟。
题目描述
写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。
示例:
输入: a = 1, b = 1
输出: 2提示:
a, b 均可能是负数或 0
结果不会溢出 32 位整数Java
class Solution { public int add(int a, int b) { //用二进制位运算 int sum=0; int carry=0; do{ sum=a^b; //1.先二进制相加,不进位,用异或代替, carry=(a & b)<<1; //2.计算进位, a=sum; b=carry; }while(b!=0); return a; }}
转载地址:http://zafdi.baihongyu.com/