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 ...
随机推荐
- 通过Matlab SDK 获取tushare数据
概要说明 版本要求:Matlab需要2016b及以上版本 接口说明:可以用help pro_api和help pro_bar查看 demo程序:请参考tushare_pro_test.m文件 程序包下 ...
- Japan 2005 Domestic Cleaning Robot /// BFS 状压 二进制位运算 结构体内构造函数 oj22912
题目大意: 输入w h,接下来输入h行w列的图 ' . ':干净的点: ' * ' :垃圾: ' x ' : 墙: ' o ' : 初始位置: 输出 清理掉所有垃圾的最短路径长度 无则输出-1 ...
- HTML5的特殊标签与IE浏览器的兼容
注释标签 ruby: 行级元素 横排显示 试图写多个汉字和注释,需要多个ruby. 直接上代码: - css样式: 页面效果: 重点标记 mark: 以灰常黄的黄色来重点标记 页面代码: 类似于进度条 ...
- Docker学习のDocker和虚拟机
最初听到Docker,是作为虚拟机来宣传的,但是它本质不是虚拟机 一.虚拟机 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. ...
- Spring注解基础学习总结
1.依赖注入注解: @Component:Bean注入到Spring容器组件(通用) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE):Bean作用域( ...
- 常用指令linux总结
linux的基础操作命令: 常见命令: man 查看帮助文档 用法:man + 命令 help 查看指定命令的用法 用法: 命令 --help(有空格) tab linux下命令与文件名补全 用法:在 ...
- ASP.NET打开项目错误:将指定的计数添加到该信号量中会导致其超过最大计数。
1.错误如图 2.解决方案 重启IIS即可,运行-> 输入IISRESET 命令 即可重启IIS,如图
- 从零开始:Mysql基于Amoeba的集群搭建
从零开始:Mysql基于Amoeba的集群搭建 准备环境 1.mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 2.amoeba-mysql-binary-2.0. ...
- Super OJ 序列计数
题意: 给出序列 a1,a2,--an(0≤ai≤109),求三元组(ai,aj,ak)(1≤i<j<k≤n)满足 ai<aj>ak 的数量. 分析: 开两个\(BIT\),分 ...
- redis 本地连接可以 远程连接不上问题
1.所连主机防火墙关一下. 1:查看防火状态 systemctl status firewalld service status iptables 2:暂时关闭防火墙 systemctl stop ...