打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
LeetCode之Sum of Two Integers

1、题目

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.

Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

2、代码实现

public class Solution {
    public int getSum(int a, int b) {
     while(b != 0)  
        {  
            int tmp =(a & b) << 1; 
            //a是每次相加,相当于和
            a = a ^ b;
            //b相当于是进位
            b = tmp;
        }  
        return a;  
     }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[leetcode] 371. Sum of Two Integers 解题报告
算法21(求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句)
LeetCode实战:最接近的三数之和
​LeetCode刷题实战561:数组拆分 I
Java 数字求和运算
【Go语言入门100题】013 计算阶乘和 (10 分) Go语言|Golang
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服