标签:

位运算

描述

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. HDU 3607 线段树+DP+离散化

    题意:从左往右跳箱子,每个箱子有金币数量,只能从矮处向高处跳,求最大可获得金币数,数据规模1<=n<=1e5. 显然是一个dp的问题,不难得出dp[ i ] = max(dp[j] )+v ...

  2. JS,JQuery,获得选中的Radio值

    1.HTML代码 <input type=" checked="checked" /><label for="a1">男< ...

  3. The linux command之环境

    一.环境 shell在环境中存储了两种数据类型:环境变量(environment variables )shell变量(shell variables).在shell中这两种变量基本没有什么不同. 此 ...

  4. python的异常捕捉

    你可能会说既然有万能异常Exception,那么我直接用上面的这种形式就好了,其他异常可以忽略 你说的没错,但是应该分两种情况去看 1.如果你想要的效果是,无论出现什么异常,我们统一丢弃,或者使用同一 ...

  5. delphi 用户可以点击格式修改进行模板修改

    过程 TlistRepAdd.Btn_GCListRepEditClick窗口 TlistRepAdd 补打流程单 1. 给用户权限 呈现出格式修改按钮 procedure TlistRepAdd.B ...

  6. 5.从物理层到MAC层

    第一层(物理层)     如何用两台电脑构成最小的局域网(LAN)?     网线的水晶头1.2和3.6脚分别起着收.发信号的作用,随意只要将水晶头做交叉线1-3.2-6交叉法,然后连接两台电脑.除了 ...

  7. hibernate4.3.8的dialect和创建SessionFactory遇到的一些问题

    好久不用hibernat,心里记着的还是hibernate3的标准,今天换成hibernate4.3.8后问题层出不穷啊... 首先是hibernate4.3.8中使用mysql方言时,hiberna ...

  8. PyQt6的在线安装与环境配置

    https://www.jianshu.com/p/185e277e0058 一,安装好Python,Pycharm 二,安装或更新pip C:\> python -m pip install ...

  9. 【JZOJ5730】【luoguP2146】【Comet OJC0396】软件包管理器

    description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  10. thinkphp 前置和后置操作

    前置和后置操作指的是在执行某个操作方法之前和之后会自动调用的方法,不过仅对访问控制器有效. 其他的分层控制器层和内部调用控制器的情况下前置和后置操作是无效的. 系统会检测当前操作是否具有前置和后置操作 ...