Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:Given a = 1 and b = 2, return 3.
方法一:用位运算模拟加法 思路1:public class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int c = a & b; //carry
a ^= b; //add
b = c << 1;
}
return a;
}
}
更多的leetcode的经典算法,查看我的leetcode专栏
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123