1. 题目描述(简单难度)

[warning] 剑指 Offer 65. 不用加减乘除做加法

2. 解法一:位运算

class Solution {
    public int add(int a, int b) {
     while(b != 0){
         int c = (a&b)<<1; // c = 进位
         a = a^b; //a = 非进位和
         b = c;  //b = 进位
     }
     return a;
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""