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. BZOJ4662 : Snow

    首先离散化,即相邻关键点之间的部分可以压成一段. 注意到区间互不包含,因此排序后每个位置的清理影响到的是一段连续区间的清理工的工作长度. 这显然可以用线段树维护,支持区间减去一个数,单点加上$inf$ ...

  2. jQuery的封装和扩展方式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 种树 & 乱搞

    题意: 在一个(n+1)*(m+1)的网格点上种k棵树,树必须成一条直线,相邻两棵树距离不少于D,求方案数. SOL: 这题吧...巨坑无比,本来我的思路是枚举每一个从(0,0)到(i,j)的矩形,然 ...

  4. Codeforces Round #210 (Div. 2) A. Levko and Table

    让对角线的元素为k就行 #include <iostream> using namespace std; int main() { int n,k; cin >> n > ...

  5. [Cocos2D-x For WP8]Sprite精灵

    精灵(Sprite)是游戏里面的角色,比如敌人,游戏里面运动的物体等等,所以精灵是游戏里面一个非常常见的概念,几乎无处不在.在Cocos2D-x里面精灵是用CCSprite类来进行表示的,它可以用一张 ...

  6. 【BZOJ】1110: [POI2007]砝码Odw

    题意 给定\(n\)个砝码和\(m(1 \le n, m \le 100000)\)个背包\((1 \le n_i, m_i \le 1000000000)\),保证对于任意两个砝码都有一个是另一个的 ...

  7. URAL 1223. Chernobyl’ Eagle on a Roof

    题目链接 以前做过的一题,URAL数据强点,优化了一下. #include <iostream> #include <cstdio> #include <cstring& ...

  8. fuser 命令的介绍

    fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息.  fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面跟一个字母表示访 ...

  9. CentOS 6.5/6.6 安装mysql 5.7 最完整版教程

    Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...

  10. jQuery $(document).ready()和window.onload

    jQuery $(document).ready()和window.onload 根据ready()方法的API说明http://api.jquery.com/ready/. 这个方法接收一个func ...