原题链接在这里: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. JS && JSON

    将Json字符串转为Json对象以下3种方式: eval('(' + str + ')'); JSON.parse(str); jQuery.parseJSON(str): 将Json对象转为字符串: ...

  2. unity3d编辑器——检视面板部分(一)

    最近在学习unity编辑器,so,记录总结一下. 以下介绍了一些简单的unity3d检视面板部分的使用技巧. using UnityEngine; using System.Collections; ...

  3. C++ 的二进制语法与语义

    /* 转载请注明出处:http://www.cnblogs.com/Martinium/p/binary_literal.html */ 二进制的语法 C/C++ 默认数字使用十进制,八进制使用前缀 ...

  4. thinphp下拉获取更多瀑布流效果

    html页面 <body> <script type="text/javascript" src="jquery.min.js">< ...

  5. 在 windows 环境下安装 redislive

    这是一篇在 windows 环境下安装 redislive 的教程! 项目地址:https://github.com/nkrode/RedisLive 配置文档:http://www.nkrode.c ...

  6. Android中的ANR

    有过Android开发经历的人都不会对ANR陌生,它和崩溃一样是程序设计的问题.本文将以较为深入的视角来介绍什么是ANR,出现场景,如何避免以及如何定位分析ANR,希望可以帮助大家在编写程序时有所帮助 ...

  7. 前台处理json字符串的几种方法(转)

    原文地址http://www.css88.com/archives/3919 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":&quo ...

  8. C#写入和读出文本文件

    C#写入和读出文本文件 写入文本文件 class WriteTextFile { static void Main() { //如果文件不存在,则创建:存在则覆盖 //该方法写入字符数组换行显示 st ...

  9. 手动编译安装docker环境,以及偶尔出现的bug

    总结安装过程如下: 前提:安装git,go,make, docker(docker中编译docker) git clone https://git@github.com/docker/docker c ...

  10. MySQL 存储过程控制语句

    变量作用域内部的变量在其作用域范围内享有更高的优先权,当执行到end.变量时,内部变量消失,此时已经在其作用域外,变量不再可见了,应为在存储过程外再也不能找到这个申明的变量,但是你可以通过out参数或 ...