题目:

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位来算进位,然后将两者相加。

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

什么样的数据可以直接移位? char、 short、 int、 long、 unsigned char、 unsigned short、 unsigned int、 unsigned long

什么样的数据不能直接移位? double、 float、 bool、 long double

LeetCode: 371 Sum of Two Integers(easy)的更多相关文章

  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 Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  6. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  7. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  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. 关于dubbo的负载均衡

    1 dubbo的集群 将同一个服务部署到多个机器上,然后全部注册到注册中心.这样的多个机器就是一个dubbo集群了. 2 dubbo的负载均衡是怎么回事 由于多台机器上都有同一个服务,因此consum ...

  2. Swift 学习笔记 (方法)

    方法 是关联了特定类型的函数.类,结构体以及枚举都能定义实例方法,方法封装了给定类型特定的任务和功能.类,结构体和枚举同样可以定义类型方法,这是与类型本身关联的方法.类型方法与 Objective-C ...

  3. 洛谷 2261 [CQOI2007]余数求和

    题目戳这里 一句话题意 求 \(\sum_{i=1}^{n} (k ~~\texttt{mod} ~~i)\) Solution 30分做法: 说实话并不知道怎么办. 60分做法: 很明显直接一遍o( ...

  4. 关联android-support-v4源码关联不上的解决办法

    在android项目中查看android-support-v4中的源码提示“Android Private Libraries which does not modified source attac ...

  5. centos7 安装jdk9 总结

    升级jdk, 从jdk8 升级到jdk9 1:卸载jdk8: 1〉 [root@localhost conf.d]# rpm -qa|grep java javapackages-tools-3.4. ...

  6. ERR:/usr/local/lib/libcrypto.so.1.0.0: no version information available

    解决方法: locate libssl.so.1.0.0   sudo rm /usr/local/lib/libssl.so.1.0.0   sudo ln -s /lib/x86_64-linux ...

  7. mooc_java Socket

    Socket通信,TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据:基于TCP协议实现网络通信的类客户端的Socket类 服务器端的ServerSocket类 -------------- ...

  8. 创建blog APP

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 什么是APP呢,Django里的APP其 ...

  9. C++之封装

    希望暴露public 希望隐藏private 对象实例化有两种方式,从栈实例化,从堆(new出来的)实例化. 以谁做什么作为核心. public 放前面,private放后面(属性可以定义为priva ...

  10. matplotlib中文显示-微软雅黑

    网上有很多方法,但是基本的是片面的. 参考1 https://tracholar.github.io/wiki/python/matplotlib-chinese-font.html 参考2 http ...