【一天一道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 ...
 
随机推荐
- 如何用Netty实现一个轻量级的HTTP代理服务器
			
为什么会想通过Netty构建一个HTTP代理服务器?这也是笔者发表这篇文章的目的所在. 其主要还是源于解决在日常开发测试过程中,一直困扰测试同学很久的一个问题,现在我就来具体阐述一下这个问题. 在日常 ...
 - Postgresql合并年月日、月份和日期左侧补零
			
在写一个统计查询的 SQL 语句时,需要根据年.月.日分组,但要求返回的字段是日期格式:yyyy年MM月dd日.刚开始我的做法是返回年.月.日,然后再手动拼接年月日,而且还要判断月份和日期是否为个位数 ...
 - Mac 上Tomcat装载
			
I recently installed Tomcat 7 and got it working with Eclipse Helios on Mac OSX Lion.Install Homebre ...
 - HashMap和ConcurrentHashMap实现原理及源码分析
			
HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表, ...
 - jvm(四):垃圾回收
			
垃圾回收我们主要从以下三个方面进行描述 垃圾对象的判断 目前判断对象为垃圾对象有两种方法:引用计数法,可达性分析法,目前普遍是的是可达性分析法 可达性分析法的实现原理: 定义gcroot一直往下找,如 ...
 - js简单备忘录
			
<section class="myMemory"> <h3 class="f-tit">记事本</h3> <div ...
 - js强大的日期格式化函数,不仅可以格式化日期,还可以查询星期,一年中第几天等
			
js强大的日期格式化,timestamp支持10位或13位的时间戳,或是时间字符串,同时支持android ios的处理,不只是日期的格式化还有其它方法,比如获 获取某月有多少天 .获取某个日期在这一 ...
 - Node.js 集群
			
稳定性: 2 - 不稳定 单个 Node 实例运行在一个线程中.为了更好的利用多核系统的能力,可以启动 Node 集群来处理负载. 在集群模块里很容易就能创建一个共享所有服务器接口的进程. var c ...
 - Data access between different DBMS and other txt/csv data source by DB Query Analyzer
			
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainl ...
 - 【Android应用开发】RecycleView API 翻译 (文档翻译)
			
. RecyclerView extends ViewGroupimplements ScrollingView NestedScrollingChild java.lang.Object ↳ ...