LintCode刷题笔记--Flip Bits
Flip Bits:
标签:位运算
题目:Determine the number of bits required to flip if you want to convert integer n to integer m.
解题思路:
给出两个数字a和b,返回两个数字中需要转换的内容
这道题主要是考察位运算的几种操作:
1.首先使用异或运算来确定在哪些位置上两个数字的二进制位不一样的数字上都填上1,得到bit=a^b.
2. 之后在与1进行与运算,如果bit的最后一位是1那么就得到1,否则为0
3. 将bit向右移动一位,比如00001001 –> 00000100, 这样每次都能检查到最后一位是0或者是1
4.反复如上过程,直到bit为0;
参考代码:
LintCode刷题笔记--Flip Bits的更多相关文章
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- 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 ...
随机推荐
- [转]浅谈C#中常见的委托
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- Django项目:CRM(客户关系管理系统)--81--71PerfectCRM实现CRM项目首页
{#portal.html#} {## ————————46PerfectCRM实现登陆后页面才能访问————————#} {#{% extends 'king_admin/table_index.h ...
- light oj 1079 01背包
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- grpc之protobuf常用语法速学
1,语法速学(1):返回商品”数组”.repeated修饰符 Repeated:是一个修饰符,返回字段可以重复任意多次(包括0次) 可以认为就是一个数组(切片) 服务端: 创建protobuf文件 s ...
- 原型模式(Prototype)(对象、克隆广告邮件)
有些对象创建过程较为复杂,而且有些时候需要频繁的创建,原型模式通过给出一个原型对象来指明所要创建的对象的类型,然后复制这个原型对象的方法创建更多同类型的对象.这就是原型模式的动机. 原型模式的主要思想 ...
- eclipse设置提示信息
1.设置 java 文件的代码提示功能 .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ () 2.设置 xml 文件的代码提示功能 打 开 ...
- map 的使用 UVA156
借用这一水题来熟悉map的操作,以前以为看过c++ primer,对map虽然不熟悉但用还是会用的,实际上手发现不是那么回事,还是有很多坑的 #include <iostream> #in ...
- Luogu P4503 [CTSC2014]企鹅QQ(字符串哈希)
P4503 [CTSC2014]企鹅QQ 题面 题目背景 \(PenguinQQ\) 是中国最大.最具影响力的 \(SNS(Social Networking Services)\) 网站,以实名制为 ...
- NYoj536 矩阵链乘
经典问题没啥说的 #include<stdio.h> #include<string.h> #define max 100+1 #define min(a,b) (a<b ...
- Http中GET和POST请求的区别
https://mp.weixin.qq.com/s/_oLnkDQn3TE7XdFWCT5Img GET请求 GET /books/?sex=man&name=Professional HT ...