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. Tensorflow细节-P199-数据集

    数据集的基本使用方法 import tempfile import tensorflow as tf input_data = [1, 2, 3, 5, 8] # 这不是列表吗,为什么书里叫数组 da ...

  2. Jenkins - Update information obtained: 不可用 ago;

    Jenkins 添加插件 jenkins plugin提示: Update information obtained: 不可用 ago: 编辑 hudson.model.UpdateCenter.xm ...

  3. noi.ac #36 模拟

    \(des\) 存在 \(1000 \times 1000\) 的矩阵,保证元素互不相同,\(2e5\) 次询问,每次询 问给定 \(x, y\) 问存在多少点 \((a, b)\) 满足该元素是 \ ...

  4. Vim初学

    实现G++编译 1,首先下载安装MinGW,下载地址在http://sourceforge.net/projects/mingw/.这个是边下载边安装的,下载完成即安装完成.我的安装目录是G:\Min ...

  5. IntelliJ IDEA 2017 JDK Tomcat Maven 配置步骤详解(一)

    要求   配置 Java基础环境(实际上应该在虚拟机linux环境下 安装CentOS 7,但是我这电脑实在承受不住了) 安装 开发工具 IntelliJ IDEA 2017.1 第一部分: JDK ...

  6. spark,hadoop集群安装注意

    安装步骤严格参看厦门大学数据实验室教程 Spark 2.0分布式集群环境搭建(Python版) 安装Hadoop并搭建好Hadoop集群环境 遇到的问题 1.ubuntu 安装后升级.python是3 ...

  7. 如何使用ArcGIS Pro发布自定义打印服务

    我们知道可以通过ArcGIS Map来发布自定义打印服务.从ArcGIS Enterprise 10.6.1版本起,打印服务的功能更加完善了,改进点包括: 支持打印矢量切片服务 改进了智能制图和颜色透 ...

  8. manjaro (arch) 安装搜狗输入法

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/sogou_input_install_in_arch_manja ...

  9. 利用斗图啦网站API批量下载表情图片

    decorator.py #!/usr/bin/env python # -*- coding: utf-8 -*- import logging import os from functools i ...

  10. Chrome浏览器控制台[DOM] Password field is not contained in a form:

    [DOM] Password field is not contained in a form: ( [DOM]密码字段不包含在form表单中) 解决方案:添加一层form标签 <div cla ...