65.不用加减乘除做加法
小于 1 分钟
65.不用加减乘除做加法
参考链接
优秀题解
class Solution {
public int encryptionCalculate(int dataA, int dataB) {
while(dataB != 0) { // 当进位为 0 时跳出
int c = (dataA & dataB) << 1; // c = 进位
dataA ^= dataB; // dataA = 非进位和
dataB = c; // dataB = 进位
}
return dataA;
}
}
作者:Krahets
链接:https://leetcode.cn/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/solutions/210882/mian-shi-ti-65-bu-yong-jia-jian-cheng-chu-zuo-ji-7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
个人理解:
将 「两个加数的加法」 转变成 「非进位 + 进位」的操作;
由于 「非进位 + 进位」也涉及到了 加法操作,因此,「非进位 + 进位」==> 下一个「非进位 + 进位」,直到 进位 为 0。