一天一道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. 数据结构之B树、B+树(一)

    B-树 什么是B-树? B树是一种查找树,我们知道,这一类树(比如二叉搜索树,红黑树等等)最初生成的目的都是为了解决某种系统中,查找效率低的问题.B树也是如此,它最初启发于二叉搜索树,二叉搜索树的特点 ...

  2. Python中模块之os的功能介绍

    Python中模块之os的功能介绍 1. os的变量 path 模块路径 方法:os.path 返回值:module 例如:print(os.path) >>> <module ...

  3. Go 完整实现版本比较 VersionCompare 函数

    [转] http://www.syyong.com/Go/Go-implementation-version-comparison-VersionCompare-function.html Versi ...

  4. easyui常见问题

    EasyUi combobox 下拉列表JS添加首/尾选择项 下拉列表获取数据后,再动态添加一项数据项,如:"<option value=''>全部</option> ...

  5. 数据库4m10d作业

    Create table student ( Sno char(15) primary key , Sname varchar(10) not null, Sage tinyint , Special ...

  6. 异常处理&RandomAccessFile&节奏感

    异常处理 异常处理方面的知识,下面是学习中记的笔记: try尝试捕获异常 catch对捕获的异常进行处理 多个catch要注意的问题: 一.顺序问题,先小后大,也就是先子类后父类.因为当异常出现的时候 ...

  7. 利用JAVA多线程来提高数据处理效率

    肿瘤大数据挖掘中经常需要处理上百亿行的文本文件,这些文件往往高达数百GB,假如文件结构简单统一,那么用sed和awk 处理是非常方便和快速的.但有时候会遇到逻辑较为复杂的处理流程,这样我一般会用JAV ...

  8. ACM Sudoku

    Sudoku是一个非常简单的任务. 具有9行9列的方形表被划分为9个较小的正方形3x3,如图所示. 在一些单元格中写入从1到9的十进制数字.其他单元格为空. 目标是填充空单元格,其中十进制数字从1到9 ...

  9. Window下通过CuteFTP与Linux虚拟机连接失败的原因总结及解决方法

    环境:虚拟机类型Redhat Enterprise 6 (1) 虚拟机是否安装了ftp服务器? 若未安装,则yum install vsftpd,并启动ftp服务 (2) 虚拟机防火墙是否已关闭? 若 ...

  10. Linux中MySQL忽略表中字段大小写

    linux 下,mysql 的表面默认是区分大小写的,windows 下默认不区分大小写,我们大多数在windows 下开发,之后迁移到linux(特别是带有Hibernate的工程),可以修改配置是 ...