LeetCode: 371 Sum of Two Integers(easy)
题目:
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)的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 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 ...
- 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 ...
- 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 -. ...
- 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) { ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [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 ...
随机推荐
- HNOI2017
本蒟蒻表示终于把$HNOI2017$全AC了... 万岁! 附上各个题的题解: $DAY1$: $T1$: BZOJ4825: [Hnoi2017]单旋 $T2$: BZOJ4826: [Hnoi20 ...
- JS中继承方式总结
说在前面:为了使代码更为简洁方便理解, 本文中的代码均将"非核心实现"部分的代码移出. 一.原型链方式关于原型链,可点击<深入浅出,JS原型链的工作原理>,本文不再重复 ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---3
字符串函数总结: 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1 void *memchr(const void *str, int c, size_t n)在参数 ...
- struts2 codebehind + actionPackages 实现Action零配置
1.jar包:struts2-codebehind-plugin-2.2.1.1.jar 2.struts.xml:<!-- codebehind中查找action的返回结果资源时的默认文件夹 ...
- 51Nod 1225 余数之和 —— 分区枚举
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 1225 余数之和 基准时间限制:1 秒 空间限制:1 ...
- 版本名称SNAPSHOT、alpha、beta、release、GA含义
Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...
- mysql八:ORM框架SQLAlchemy
阅读目录 一 介绍 二 创建表 三 增删改查 四 其他查询相关 五 正查.反查 一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进 ...
- 利用Python+阿里云实现DDNS(动态域名解析)
引子我想大家应该都很熟悉DNS了,这回在DNS前面加了一个D又变成了什么呢?这个D就是Dynamic(动态),也就是说,按照传统,一个域名所对应的IP地址应该是定死的,而使用了DDNS后,域名所对应的 ...
- openfire插件开发环境
创建java工程 SamplePlugin: package com.hoo.server.plugin; import java.io.File; import org.jivesoftware.o ...
- codeforces 651E E. Table Compression(贪心+并查集)
题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...