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.

只能位运算:

0x01101011 ^

0x00111001

----------

0x01010010

0x01101011 &

0x00111001

---------

0x00101001 <<1

---------

0x01010010

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

迭代解法:

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

python版本:不明白 ~(a ^ mask) 是。。。

class Solution(object):def getSum(self, a, b):""":type a: int:type b: int:rtype: int"""# 32 bits integer maxMAX = 0x7FFFFFFF# 32 bits interger minMIN = 0x80000000# mask to get last 32 bitsmask = 0xFFFFFFFFwhile b != 0:# ^ get different bits and & gets double 1s, << moves carrya, b = (a ^ b) & mask, ((a & b) << 1) & mask# if a is negative, get a's 32 bits complement positive first# then get 32-bit positive's Python complement negativereturn a if a <= MAX else ~(a ^ mask)

转载于:https://www.cnblogs.com/bonelee/p/8593090.html

leetcode 371. Sum of Two Integers相关推荐

  1. leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...

  2. Leetcode PHP题解--D84 371. Sum of Two Integers

    D84 371. Sum of Two Integers 题目链接 371. Sum of Two Integers 题目分析 相加给定的两个数,但不能使用+或-运算符. 思路 可以用二进制的与运算完 ...

  3. Leetcode:371.Sum Of Two Integer

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

  4. LeetCode之Sum of Two Integers

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

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

  6. C#LeetCode刷题之#371-两整数之和(Sum of Two Integers)

    问题 不使用运算符 + 和 - ,计算两整数 a .b 之和. 输入: a = 1, b = 2 输出: 3 输入: a = -2, b = 3 输出: 1 Calculate the sum of ...

  7. sum of two integers

    https://leetcode.com/problems/sum-of-two-integers/ Calculate the sum of two integers a and b, but yo ...

  8. AOJ0008 Sum of 4 Integers【暴力】

    Sum of 4 Integers Aizu - 0008 Write a program which reads an integer n and identifies the number of ...

  9. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

最新文章

  1. 腾讯优图开源业界首个3D医疗影像大数据预训练模型
  2. oracle打印乘法口诀,ORACLE中实现输出乘法口诀
  3. 字节跳动面试官问我看过哪些源码,然后就没有然后了
  4. java:自定义数据库连接池
  5. Android WebView使用基础
  6. 高光谱提取薯叶特征波长
  7. 33-高级特性之generator(1)
  8. oracle update命令未正确结束,ORA-00933: SQL 命令未正确结束处理办法
  9. 深度学习---卷积神经网络+tensorflow实现
  10. python计算定积分_python 求定积分和不定积分示例
  11. 数仓工具—Hive语法之窗口函数first_value和last_value(12)
  12. 尼采:快乐的知识(上)
  13. mysql主从配置干什么_mysql主从配置之slave_exec_mode=IDEMPOTENT详解
  14. 3D MAX界面操作教程及渲染技巧
  15. 有关getAttribute()方法和setAttribute()方法的说明
  16. 浅谈大数据如何管理与分析
  17. 2022-2028全球气动吸尘器行业调研及趋势分析报告
  18. 题源阅读5-6篇、听力1-3篇笔记
  19. 爬虫日记(28):scrapy使用中间件调用浏览器
  20. Hadoop_day04学习笔记

热门文章

  1. java中并行_[JAVA] 12. Java中的并行Concurrency
  2. ASCII、Unicode、GBK、UTF-8之间的关系
  3. C API向MySQL插入批量数据的快速方法——关于mysql_autocommit
  4. iovec结构体定义及使用
  5. vivo检测自启动权限_VIVO手机不如苹果?那是你不知道还有这几个功能,用了就离不开...
  6. foreach php,详解PHP中foreach的用法和实例
  7. stm32 内部sram大小_STM32第三天
  8. android测试 课程设计,超星尔雅Android开发课程设计章节测试答案
  9. java中min用法,java11教程--类MinguoDate用法
  10. win10网络不出现计算机列表,win10网络发现已关闭网络计算机和设备不可见怎么办?...