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、如此循环

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

  

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. sql跨电脑导数据

    启用Ad Hoc Distributed Queries: reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigu ...

  2. uva10106(大数乘法)

    public class Product { public static void main(String[] args){ Scanner sc = new Scanner(new Buffered ...

  3. (转)MySQL提示“too many connections”的解决办法

    link:http://www.cfp8.com/mysql-prompt-too-many-connections-solution.html 今天生产服务器上的MySQL出现了一个不算太陌生的错误 ...

  4. java语言特性概述

    一.前言 我们都知道java是面向对象的编程,其中四个基本特性:抽象.封装.继承.多态.这四个特性,概括起来可以这么理解,抽象.封装.继承是多态的基础,多态是抽象.封装.继承的表现. 二. JAVA ...

  5. 关于Div的宽度与高度的100%设定

    http://www.cnblogs.com/clare-zhang/archive/2011/08/26/2154220.html 正像你所知道的那样,设置DIV大小的有两个属性width和heig ...

  6. UVA 11609 - Teams(二项式系数)

    题目链接 想了一会,应该是跟二项式系数有关系,无奈自己推的式子,构不成二项式的系数. 选1个人Cn1*1,选2个人Cn2*2....这样一搞,以为还要消项什么的... 搜了一下题解,先选队长Cn1,选 ...

  7. iOS 开发小结

    一,经历 1> 在编写以前有过的类似的新功能时,如果以前的开发人员没有写明明确的注释和开发需求,一定要仔细阅读所有代码,每一句代码都有它存在的意义. 2> 例如,只以为是[self.ful ...

  8. Maven Repository

    The usefully link for Maven Reponsitory display as below: http://mvnrepository.com/ For example, To ...

  9. Iterator用法

    <% List<Emp> all=DAOFactory.getIEmpDAOInstance().findAll(keyWord); Itrator<Emp> iter= ...

  10. concat,reverse

    1.concat 连接,拼接 <script> var arr1=[1,2,3]; var arr2=[4,5,6]; var arr3=[7,8,9]; alert(arr1.conca ...