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. Hi,this is May.

    “山有木兮木有枝 心悦君兮君不知” 当一个现在的人正在思念过去的人,世间的一切也都会变成过去的样子. 声色的娱乐,本来就如闪电的光.击石的火.男女欢合不过埋香葬玉.赋别鹤离鸾之曲,臂膀一曲一伸的工夫罢 ...

  2. Kylin 1 背景、历史与使命

    1.1 背景和历史 今天,大数据领域的发展如火如荼,各种新技术层出不穷,整个生态欣欣向荣.作为大数据领域最重要的技术——Apache Hadoop,从诞生至今已有10周年.它最初只是致力于简单的分布式 ...

  3. (尚003).Vue_模板语法

    1.双大括号表达式 2.指令一:强制数据绑定 3.指令二;绑定事件监听 test003.html<!DOCTYPE html><html lang="en"> ...

  4. undefined null 与 字符串相加

        在进行一个字符串的判断的时候. 如下一段内容 const queryObj = {}; const  str = queryObj.criteriaStr + "" con ...

  5. @RestController和@GetMapping

    @RestController 可以代替@Controller使用,使用了@RestController的控制器默认所有请求方法都用了@ResponseBody注解. @GetMapping(&quo ...

  6. Loadrunner11录制过程中报错:由于另一个程序正在运行中,此操作无法完成

    第一次安装LR11录制没有问题,第二次录制也没有问题,后面第二天录制就出现了如下截图的现象  报错内容:由于另一个程序正在运行中,此操作无法完成.请选择“切换到”来激活正在运行中的的程序,并更正问题 ...

  7. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  8. BS4库详解

    from bs4 import BeautifulSoup html = """ <html><head><title>This is ...

  9. 2018-2019 20165226 Exp 8 Web基础

    2018-2019 20165226 Exp 8 Web基础 目录 一.实验内容说明及基础问题回答 二.实验过程 1.Web前端:HTML 2.Web前端j:avascipt 3.Web后端:MySQ ...

  10. bugku web所有writeup_超详细讲解_持续更新

    首先说一下我的主用工具,在windows下,主要是用这些,用到其他特定的工具会在题里说. 0.浏览器:火狐,配合Max hackbar插件 (这个是免费的) 1.抓包改包:burpsuite.http ...