一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

Example:

Given a = 1 and b = 2, return 3.

(二)解题

题目大意:不用+或者-实现两个整数的加法

解题思路:不用+或者-,就自然想到位运算,无非就是与或非来实现二进制的加法

首先,我们来看一位二进制的加法和异或运算

A B A&B A^B
0 0 0 0
1 0 0 1
0 1 0 1
1 1 1 0

与运算可以算出每一位上的进位,异或运算可是算出每一位相加的值。与和异或的值就等于加法后的值

于是,我们想到对于多位数的加法,异或运算也能求出每一位上相加的值(没有进位),然后考虑到进位,与得出每一位上面的进位

每次进位左移一位与没有进位的值再求异或,一直算到进位为0,即可得出最后的相加的值。

class Solution {
public:
    int getSum(int a, int b) {
        int sum = a;
        int carry = b;
        while(carry!=0)
        {
            int temp = sum^carry;//没有考虑进位的相加值
            carry = (sum&carry)<<1;//进位左移一位,等待下一次循环加到sum中
            sum = temp;
        }
        return sum;
    }
};

【一天一道LeetCode】#371. Sum of Two Integers的更多相关文章

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

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

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

  3. 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 ...

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

  5. LeetCode: 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 -. ...

  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. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

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

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. Ubuntu 16.04安装JDK/JRE并配置环境变量

    作为一个Linux新手,在写这篇文章之前,安装了几次jdk,好多次都是环境变量配置错误,导致无法登录系统.经过几天的研究,今天新装系统,从头来完整配置一遍 系统版本:Ubuntu 16.04 JDK版 ...

  2. 笔记8 AOP练习2

    场景描述: 一张唱片有好多磁道,假设每个磁道只有一首歌,现在需要记录每首歌的播放次数,然后输出. 主要业务:歌曲播放 辅助功能:记录播放次数(切面) 1.创建唱片接口,CompactDiscs.jav ...

  3. Python 中的 if __name__ == '__main__' 该如何理解

    __name__ 表示当前模块名, __main__ 表示正在运行的模块名. if __name__ == '__main__' 这句话的意思就是,当模块被直接运行时,以下代码块将被运行,当模块是被导 ...

  4. Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现

    Tensorflow 实现 A Tensorflow implementation of CapsNet(Capsules Net) in Hinton's paper Dynamic Routing ...

  5. python2.7练习小例子(三)

        3):题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?     程序分析:假设该数为 x.1.则:x + 100 = n2, x + 100 + ...

  6. Spring中<context:annotation-config/>

    最近在研究Spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Sp ...

  7. Linux的发行版,不同发行版之间的联系和区别

    Linux 主要作为Linux发行版(通常被称为"distro")的一部分而使用.这些发行版由个人,松散组织的团队,以及商业机构和志愿者组织编写.它们通常包括了其他的系统软件和应用 ...

  8. Python小代码_9_求水仙花数

    for i in range(100, 1000): ge = i % 10 shi = i // 10 % 10 bai = i // 100 if ge ** 3 + shi ** 3 + bai ...

  9. Matlab 编译EXE

    环境:vs2013  matlab2015b 一.生成独立可执行的程序(exe文件)步骤1.设置编译器.在matlab命令行输入mbuild –setup以及mex –setup,选择安装的c编译器. ...

  10. 关于使用Git的几点小技巧

    告诉git忽略对已经纳入版本管理的文件a的修改,git会一直忽略此文件直到重新告诉git可以再次跟踪此文件: git update-index --assume-unchanged a 告诉git恢复 ...