【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:不用+或者-实现两个整数的加法
解题思路:不用+或者-,就自然想到位运算,无非就是与或非来实现二进制的加法
首先,我们来看一位二进制的加法和异或运算
| A | B | A&B | A^B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 0 |
与运算可以算出每一位上的进位,异或运算可是算出每一位相加的值。与和异或的值就等于加法后的值
于是,我们想到对于多位数的加法,异或运算也能求出每一位上相加的值(没有进位),然后考虑到进位,与得出每一位上面的进位
每次进位左移一位与没有进位的值再求异或,一直算到进位为0,即可得出最后的相加的值。
class Solution {
public:
int getSum(int a, int b) {
int sum = a;
int carry = b;
while(carry!=0)
{
int temp = sum^carry;//没有考虑进位的相加值
carry = (sum&carry)<<1;//进位左移一位,等待下一次循环加到sum中
sum = temp;
}
return sum;
}
};
【一天一道LeetCode】#371. Sum of Two Integers的更多相关文章
- 剑指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: 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 -. ...
- 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 解题报告(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 ...
随机推荐
- C语言程序设计第一次作业 冯亚杰
(一)实验总结 求圆面积和周长 (1)题目 输入圆的半径,计算圆的周长和面积. (2)流程图 (3)测试数据及运行结果 测试数据1:r=7 运行结果: 测试数据2:r=5 运行结果: (4)实验分析 ...
- iOS 搜索记录
需求描述: 使用单独的搜索界面, 提供用户进行搜索并留下搜索记录. 搜索记录可以提供用户进行再次搜索, 或者把搜索记录清空. 方案和技术点: 存储方式使用 NSUserDefaults, 把对应的字段 ...
- c++银行家算法
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ...
- ubuntu14.04拼音输入法问题的解决方法
14.04的自带的拼音输入法,打字的时候你会发现有很大的问题,打不出来一个完整的字. 解决方法: ibus-daemon -drx //重启ibus进程 在终端里输一次这个命令就ok了. 或者装个 ...
- 剑指架构师系列-MySQL调优
介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...
- Python3 错误和异常
Python有两种错误很容易辨认:语法错误和异常. 语法错误 Python 的语法错误或者称之为解析错,是初学者经常碰到的,如下实例 >>> while True print('He ...
- About Windows 10 April 2018 Update
在四月的最后一天,微软终于正式发布了 Windows 10 的又一次重大更新,并命名为 Windows 10 四月更新,轮压哨,我软确实谁也不服:再晚一天,我软改名部门恐怕又要发挥作用了,毕竟我软存在 ...
- JVM初探- 使用堆外内存减少Full GC
JVM初探-使用堆外内存减少Full GC 标签 : JVM 问题: 大部分主流互联网企业线上Server JVM选用了CMS收集器(如Taobao.LinkedIn.Vdian), 虽然CMS可与用 ...
- Bootstrap3 代码-变量
通过 <var> 标签标记变量. y = mx + b <var>y</var> = <var>m</var><var>x< ...
- 在安卓代码中dp 和 sp 换算px
/** * 单位转换工具 * * @author carrey * */ public class DisplayUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @p ...