LeetCode——Sum of Two Integers
LeetCode——Sum of Two Integers
Question
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.
Solution
异或操作有累加的效果,但是没有进位的效果。什么时候进位呢? 同时为1的时候进位,往前进一位,所以用交集操作,再移位,表示进位。
Answer
class Solution {
public:
int getSum(int a, int b) {
int sum = a;
while(b != 0) {
sum = a ^ b;
b = (a & b) << 1;
a = sum;
}
return sum;
}
};
LeetCode——Sum of Two Integers的更多相关文章
- [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 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 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 ...
- 剑指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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
随机推荐
- 51、自定义View基础和原理
一.编写自己的自定义View最简单的自定义View,继承View通过覆盖View的onDraw方法来实现自主显示利用Canvas和paint来绘制显示元素(文字,几何图形等) <com.myvi ...
- 一个最简单的JStorm例子
最简单的JStorm例子分为以下几个步骤: 1.生成Topology Map conf = new HashMp(); //topology所有自定义的配置均放入这个Map TopologyBuild ...
- phpstorm 9.0最新 注册码
phpstorm注册码: User Name:newasp 01.License Key: ===== LICENSE BEGIN ===== 14617-12042010 00001xrVkhnPu ...
- javascript中innerHTML的问题
获取document.getElementById()时,使用innerHTML获取标签获取内容,要注意: 1.要让内容先加载完,才可以使用,不然获取的是空,使用:window.onload = .. ...
- 安全必须作为基础,融入到产品开发过程——By Me
安全必须作为基础融入,让公司兄弟姐妹具备足够的安全意识,且逐渐具备必要的安全经验,带着安全思维去执行产品的架构设计.研发.测试.运维(甚至完整的产品生命周期),这是一种有远见.负责人的产品过程!! 下 ...
- servlet 文件下载
[本文简介] 一个servlet 文件下载 的简单例子. [文件夹结构] [java代码] package com.zjm.www.servlet; import java.io.BufferedIn ...
- sql server性能调优
转自:https://www.cnblogs.com/woodytu/tag/%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98%E5%9F%B9%E8%AE%AD/defaul ...
- [译转]深入理解LayoutInflater.inflate()
原文链接:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ 译文连接:http://bl ...
- 002-主流区块链技术特点及Hyperledger Fabric V0.6版本特点
一.主流区块链技术特点 二.HyperLedger子项目 三.Hyperledger fabric架构 V0.6逻辑架构: V0.6区块链网络 对应的0.6版本的运行时架构: 0.6版本的架构特点是: ...
- HadoopHA简述
1 概述 在hadoop2.0之前,namenode只有一个,存在单点问题(虽然hadoop1.0有 secondarynamenode,checkpointnode,buckcupnode这些,但是 ...