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.

一般步骤:

1、计算不用进位的位置,使用异或

2、计算进位,算数与操作,计算完后左一一位

3、如此循环

  1. class Solution {
  2. public:
  3. int getSum(int a, int b) {
  4. int carry = 0;
  5. while(b)
  6. {
  7. a = a^ b;
  8. carry = a&b;
  9. b = carry << 1;
  10.  
  11. }
  12. return a;
  13. }
  14. };

  

leetcode371. Sum of Two Integers的更多相关文章

  1. 每天一道LeetCode--371. Sum of Two Integers

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

  2. [Swift]LeetCode371. 两整数之和 | 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 两数之和

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

  4. LeetCode Sum of Two Integers

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

  5. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

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

  7. 【LeetCode】Sum of Two Integers

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

  8. 371. Sum of Two Integers -- Avota

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

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

随机推荐

  1. 2016年AR行业十大热点事件汇总

    2016年即将接近尾声,增强现实在今年完成了里程碑式的跃进.无论是从新玩法的开发还是从大众接受度,以及行业巨头的青睐,无不证明这AR的无线潜力,故而,2016年算是AR的崛起之年. 纵观全年AR新闻事 ...

  2. android NDK入门 windows下安装cygwin

    一.Android NDK环境简介 Android NDK 是运行于Android 平台上的Native Development Kit 的缩写. Android 应用开发者可以通过NDK 调用C 或 ...

  3. Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guy

    注意数据范围即可 #include <iostream> #include <vector> #include <algorithm> using namespac ...

  4. [题解]扫雷Mine

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrft.html 1088 ...

  5. TC SRM 591

    表示第一次在div1,我要记录一下... 木有挂0,第一题不怎么难的,读题读了20分钟,又想了20分钟,时间有点长,然后各种小心,然后得了140分.... 后两个题,根本木有看,貌似做出来的也不多.. ...

  6. SQL Server2008清空日志文件

    USE[master] GO ALTER DATABASE mydbname SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE mydbname S ...

  7. Oracle connect by 树查询之三(超详细)

    查找员工编号为7369的领导: 1 SELECT LEVEL,E.* FROM EMP E CONNECT BY PRIOR E.MGR = E.EMPNO START WITH E.EMPNO = ...

  8. LUA 配置 运行 异常的备忘录

    1. 抛异常“lua: LuaInterface: cannot instantiate interpreter”,如图: 目前,重新生成dll之后,会多生成一个lua51.dll到\Lua\5.1\ ...

  9. .net后台获取HTML中select元素选中的值

    前台: <select id="Province" name="Province" class="select"></se ...

  10. Digital Imaging Processing 数字图像处理

    8-Bit and 16-Bit Images 关于量化压缩与量化补偿 RGB Bayer Color分析 彩色CCD/CMOS的格式和计算机中的读取格式