标签:

位运算

描述

Write a function that add two numbers A and B. You should not use + or any arithmetic operators.

解题思路:

利用位运算来解决A+B的问题,可以将此问题转化为解决不进位相加和进位(carry bit)的两部分问题:

1. 首先是不进位相加:_A = A^B 先对A和B进行异或运算(XOR manupitation) , A 和B 中两位不相同的变为1,相同的变为 0, 同为1是不进位

2. 其次是进位  _B =(A&B)<<1

所以 A+B = A^B + (A&B)<<1

3.在循环中(A&B)<<1 的目的是将要进位的1一直向左移动,之后与新生成的_A进行异或运算,来进行进位运算的模拟(这一步就是进位)

4. 直到将所有的1全部向左移位为0

参考代码:

LintCode刷题笔记-- A+B problem的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  9. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

随机推荐

  1. 面试系列32 集群部署时的分布式session如何实现

    session是啥?浏览器有个cookie,在一段时间内这个cookie都存在,然后每次发请求过来都带上一个特殊的jsessionid cookie,就根据这个东西,在服务端可以维护一个对应的sess ...

  2. Ubuntu环境下Postgres源码文件编译安装步骤

    step1:官网下载postgres源码 URL:https://www.postgresql.org/ftp/source/ step2:解压源码文件 tar -zxvf postgresql-12 ...

  3. Leetcode274.H-IndexH指数

    原题的中文翻译不是很好,所以给出英文版. Given an array of citations (each citation is a non-negative integer) of a rese ...

  4. splice用法

    splice()方法给数组添加内容,返回新的数组 splice()方法替换数组一项内容,返回新的数组 如果添加进数组的元素个数不等于被删除的元素个数,数组的长度会发生相应的改变. 比如:从第 2 位开 ...

  5. JAVA数据结构之二叉树

    用树作为存储数据的结构兼具像数组一样查询速度快和像链表一样具有很快的插入和删除数据项的优点 我们用圆点表示节点,连接圆的直线表示边如下图所示就表示了一颗树,接下来我们讨论的二叉树即每个节点最多只有两个 ...

  6. leetcode-157周赛-5216-统计元音字母序列的数目

    题目描述: 方法:倒推 class Solution(object): def countVowelPermutation(self, n): MOD = 10 ** 9 + 7 a=e=i=o=u= ...

  7. colormap 参数及对应色卡

    [参考] [1]matlab帮助文档

  8. day13 python-001 简介及循环、判断、复制等

    Python之路,Day1 = Python基础1 本节内容 1.Python历史简介以及安装.环境变量的配置: 2.Pycharm的简单设置: 3.变量的使用及定义: 4.获取键盘输入及打印: 5. ...

  9. 最大流——poj1459

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  10. Linux课程---13、linux中任务计划介绍(任务计划分类)

    Linux课程---13.linux中任务计划介绍(任务计划分类) 一.总结 一句话总结: 1.一次性任务计划:at 2.周期性任务计划:crontab 1.linux中如何添加一次性任务计划? at ...