LintCode刷题笔记-- A+B problem
标签:
位运算
描述
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的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
随机推荐
- grep 的一些常用用法
打印匹配到的上下5行 grep -C 5 'root' /etc/passwd 上下5行 grep -A 5 'root' /etc/passwd afte ...
- jpa现有接口方法说明 (转https://www.cnblogs.com/rulian/p/6557471.html)
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- 19-MySQL-Ubuntu-数据表的查询-自关联(八)
自关联 转自:https://blog.csdn.net/hubingzhong/article/details/81277220
- ubontu 16 下的 pylon5 安装
想用wine,然后走了很多弯路.后来发现正确的安装和配置pylon即可.注意32位和64位不可以混淆. If you choose to install in a different director ...
- [转载]Spring AOP 深入剖析
转载自 http://www.cnblogs.com/digdeep/p/4528353.html 多谢@digdeep AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有 ...
- Docker的概念及基本用法
Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源.Docker提供了一种在安全.可 ...
- mysql 12章_MySQL数据库的高级管理
一. 用户管理 Root用户是MySQL数据库管理系统中的系统管理员,但在实际开发过程中通常需要根据不同的开发者分配不同的用户,这样有利于用户的管理和维护. . 用户的创建: ) 方式1:使用MySQ ...
- tp5.0x代码执行
1.拿到站首先平复一下心情 看了一下robots.txt结构像dedecms,网站还存在CDN,日了狗看到这里其实都想放弃来着,懒癌晚期,然后接着使用云悉平台走了一波,看了一下得到真实IP,看来只给w ...
- 1分钟k线图能反映什么?(转)
对于投资者特别是短线操作者来讲,应该重视1分钟K线图,但是并不是所有的股票都能通过1分钟K线图看出名堂来,比如一些小盘股,盘子较轻,很容易上蹿下跳.仅用1分钟K线图分析其上证指数,很难研判大盘当日的高 ...
- Es567严格模式
Es5 严格模式 http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 除了正常运行模式(混杂模式),ES5添加了第二种 ...