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));
}
}

LeetCode_371. Sum of Two Integers的更多相关文章

  1. [LeetCode] Sum of Two Integers 两数之和

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

  2. LeetCode Sum of Two Integers

    原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...

  3. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  4. LeetCode 371. Sum of Two Integers

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

  5. leetcode371. Sum of Two Integers

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

  6. 【LeetCode】Sum of Two Integers

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

  7. 每天一道LeetCode--371. Sum of Two Integers

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

  8. 371. Sum of Two Integers -- Avota

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

  9. Leetcode 371: Sum of Two Integers(使用位运算实现)

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

随机推荐

  1. Django --- 多对多关系创建,forms组件

    目录 多对多三种创建方式 1.系统直接创建 2.自己手动创建 3.自己定义加与系统创建 forms组件 1. 如何使用forms组件 2. 使用forms组件校验数据 3. 使用forms组件渲染标签 ...

  2. 物体检测方法(1) - YOLO 详解

    最近遇到一些卡证识别的检测问题,打算先把理论知识梳理一下,随后还会梳理一版代码注释. 以前的region+proposal来检测的框架,这一系列速度和精度不断提高,但是还是无法达到实时.存在的主要问题 ...

  3. Java方法注意事项

    使用方法的注意事项: 1.方法应该定义在类中,方法中不能再定义方法,也就是不能嵌套定义,但方法可以中可以调用方法 2.方法定义的前后顺序无所谓,执行的先后顺序只与调用有关 3.方法定义之后不会执行,如 ...

  4. 编程判断输入的字符是否为‘y’或‘Y’,若是,则输出‘yes’,否则输出‘no’

    #include<stdio.h>void main(){ char ch; ch=getchar(); ch == 'y' || ch == 'Y' ? printf("yes ...

  5. asp.net MVC 使用wifidog 协议实现wifi认证

    在网上看到的很多实现的wifidog 协议一般都是PHP 的,了解一下PHP 但是比较喜欢.net ,所以实现了简单的一个进行登录认证的功能 (好多协议中的功能目前没有实现) 1. 开发环境(vs20 ...

  6. 虚拟环境安装及Hello World

    学习文章引自:  http://www.pythondoc.com/flask-mega-tutorial/helloworld.html 1.安装项目需要的工具包 pip install flask ...

  7. PHP利用执行操作符;模拟shell命令

    $out = `ls -la`; echo '<pre>'.$out.'</pre>'; 输出结果: total 10 drwxrwxrwx 1 root root 4096 ...

  8. 利用fgetc合并2个源文件的内容,到一个新的文件中

    #include <stdio.h> #include <stdlib.h> //功能: 合并2个源文件的内容,到一个新的文件中 int main(int a,char *ar ...

  9. Code Chef October Challenge 2019题解

    传送门 \(MSV\) 设个阈值搞一搞就行了 //quming #include<bits/stdc++.h> #define R register #define pb emplace_ ...

  10. Python中文件读写read,readline,readlines函数的区别?

    read 每次会读取整个文件 readline 每次读取一行信息 readlines 读取整个文件返回一个列表,列表每个元素代表一行