标签:

位运算

描述

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. vue中excal表格的导入和导出

    注意:vue中要实现表格的导入与导出,首先要install两个依赖, npm install -S file-saver xlsx  和  npm install -D script-loader.其 ...

  2. SpringMVC(AbstractController,拦截器,注解)

    1.Controller接口及其实现类 Controller是控制器/处理器接口,只有一个方法handleRequest,用于进行请求的功能处理(功能处理方法),处理完请求后返回ModelAndVie ...

  3. 知识图谱+Recorder︱中文知识图谱API与工具、科研机构与算法框架

    目录 分为两个部分,笔者看到的知识图谱在商业领域的应用,外加看到的一些算法框架与研究机构. 文章目录 @ 一.知识图谱商业应用 01 唯品金融大数据 02 PlantData知识图谱数据智能平台 03 ...

  4. 【ASP.Net Core】不编译视图文件

    原文:[ASP.Net Core]不编译视图文件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aqtata/article/details/818 ...

  5. 【bug】vue同一组件使用

    vue使用同一个组件渲染,进行切换过程中会存在数据保存的情况. 比如路由切换,进行渲染的页面来自同一个组件,这个时候,要在监听路由的时候,将数据重新初始化

  6. Android系统开发 编译系统签名的APP

    前言 一般情况下,我们使用的签名都是自己生成的Java签名来编译APP. 但是,如果需要开发一些特定设备的APP(对权限有更高的要求,需求一些系统基本的权限,比如让APP可以控制设备的休眠),那就需要 ...

  7. 【Servlet】Servlet监听器

    一.Servlet监听器的概念 Servlet监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext.HttpSession和ServletRequest等域对象的创建与销 ...

  8. 大杀器Bitset

    其实并不怎么会用,有一次有位学长提到了这个名字,就这么取题目了. 1.BZOJ 3687 简单题 求子集的算术和的异或和 http://www.lydsy.com/JudgeOnline/proble ...

  9. go包flag系统包简单使用

    一.代码 package main import ( "flag" "fmt" ) //定义命令行参数,这个mode是内存地址,参数1是命令行名称,参数2是命令 ...

  10. 20175323《Java程序设计》第四周学习总结

    教材学习内容总结 我用幕布记录学习过程和思路,下面是我这章的知识框架总结https://mubu.com/doc/ffMhY6FVc0 教材学习中的问题和解决过程 问题1:教材121页的例六Examp ...