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

题目:

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

题解:

两个数79和16 相加,不考虑进位相加得85, 只考虑进位进位是10, 85+10 = 95正好是结果.

二进制表示时, 不考虑进位0+0=0, 1+0=1, 0+1=1, 1+1=0, 其实就是xor.

只考虑进位0+0=0, 1+0=0, 0+1=0, 1+1=1, 其实就是&.

进位是放到更前以为,所以需要左移动一位, <<1.

可递推调用.

Time Complexity: O(1).

Space: O(1).

AC Java:

 public class Solution {
public int getSum(int a, int b) {
if(b == 0){
return a;
}
int sum = a^b;
int carry = (a&b)<<1;
return getSum(sum, carry);
}
}

也可Iteration.

Time Complexity: O(1).

Space: O(1).

AC Java:

 public class Solution {
public int getSum(int a, int b) {
while(b != 0){
int carry = (a&b)<<1;
a ^= b;
b = carry;
}
return a;
}
}

类似Add Two Numbers.

LeetCode Sum of Two Integers的更多相关文章

  1. LeetCode——Sum of Two Integers

    LeetCode--Sum of Two Integers Question Calculate the sum of two integers a and b, but you are not al ...

  2. [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 ...

  3. Leetcode: Sum of Two Integers && Summary: Bit Manipulation

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

  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. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  6. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  7. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  8. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  9. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

随机推荐

  1. 与你相遇好幸运,Postgresql和postgis安装

    笔者开发环境: windows 7 x86_64 一开始安装的是官网最新版 PostgreSQL 9.6 ,安装成功 之后安装PostGIS Bundle 2.2 for PostgreSQL x64 ...

  2. [BI项目记]-文档版本管理笔记

    代码的版本管理程序员们有专门的工具,那么作为项目管理人员如何进行文档版本的管理呢,此篇介绍如何通过SharePoint进行文档版本管理. 在没有SharePoint的时代我们如何管理版本呢?通常我们会 ...

  3. github入门教程

    1.下载git windows 客户端 https://git-for-windows.github.io/ 2.配置github 安装windows客户端以后,在里面输入如下命令 首先在本地创建ss ...

  4. 对LR关联的一些理解

    从接触LR关联的迷茫,到现在略有感悟,小记期间的一些理解. 一开始认识关联是在LR自带学习例子,需要关联session,也知道了自动关联,手动关联以及边录制边关联. 在使用关联的过程中,也学习了web ...

  5. cmd中编译java

    cmd定位到.java文件所在位置: 注意.java文件名应与类名相同. javac xxx.java:编译(生成.class文件): java xxx:运行(执行.class文件): 若类间相互调用 ...

  6. 【javascript学习——《javascript高级程序设计》笔记】DOM操作

    DOM(文档对象模型)是针对HTML和XML文档的一个API(应用程序编程接口).DOM描绘了一个层次节点树,允许开发人员添加.移除和修改. 1.节点层次 <html> <head& ...

  7. 浏览器-10 Chromium 移动版

    移动版 chromium 的iOS版和Android是为两个流行的移动操作系统设计的, UI方面进行了 较大的重新设计; 两者从外观上看颇为相似,但是其内部的渲染引擎的差别非常的大,原因在于iOS对应 ...

  8. jquery 使用需要注意

    jquery选择器,层选择器等多个选择器,jquery生成对象,jquery遍历对象, jquery ajax调用不要进行方法封装返回值方式调用,会取不到值. jquery使用要注意很多细节点才能将其 ...

  9. jenkins和hudson---打酱油的日子

    自动化构建:Jenkins起源于Hudson.Hudson在商业软件的路上继续前行,而Jenkins则作为开源软件,从hudson分支出来. 因此现在的jenkins和hudson非常类似,但是随着二 ...

  10. 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用

    一.前言:  A.Arduino简介 Arduino是由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Banzi.David Cuartielles.Tom Igoe.Gianluc ...