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