371. Sum of Two Integers

Easy

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3

Example 2:

Input: a = -2, b = 3
Output: 1
package leetcode.easy;

public class SumOfTwoIntegers {
public int getSum(int a, int b) {
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
int sum = a ^ b;
int carry = (a & b) << 1;
return getSum(sum, carry);
} @org.junit.Test
public void test() {
System.out.println(getSum(1, 2));
System.out.println(getSum(-2, 3));
}
}

最新文章

  1. haproxy利用ACL规则封禁自定义IP地址拒绝访问
  2. session过期时间
  3. Java学习笔记(六)
  4. CSS 魔法系列:纯 CSS 绘制各种图形《系列五》
  5. Android SDK Android NDK Android Studio 官方下载地址
  6. odbc错误信息一览表
  7. PHP开发APP接口(一)
  8. angular 基础练习
  9. web.xml 详解contextConfigLocation 转
  10. 利用CSS边框合并属性打造table细边框
  11. C# Excel嵌入到Winform
  12. IdentityServer4实战 - 与API单项目整合
  13. SpringMVC实现文件下载时,请求路径中的扩展名被省略
  14. mui上拉刷新+下拉加载
  15. UVa LA 4636 Cubist Artwork 难度: 0
  16. day06-三元表达式
  17. 第19章:MongoDB-聚合操作--聚合管道--$limit+$skip
  18. Win10易升-在线升级工具
  19. TCP详解 (1)
  20. gem install redis报错解决

热门文章

  1. Kylin 1 背景、历史与使命
  2. 014_Python3 循环语句
  3. 贪心算法训练(九)——Best Cow Line(字典序最小问题)
  4. fluent中截取任意面的数据
  5. LeetCode 第 154 场周赛
  6. 2019秋季 关于C语言指针等探索
  7. [Linux] 60s快速分析Linux性能
  8. numpy中的mean()函数
  9. 【MySQL】Mac通过brew安装的MySQL重启方法
  10. 干货满满!10分钟看懂Docker和K8S(转)