Flip Bits:

标签:位运算

题目:Determine the number of bits required to flip if you want to convert integer 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的更多相关文章

  1. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

  2. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  3. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  4. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  5. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  6. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  7. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  8. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  9. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

随机推荐

  1. hdu1527

    hdu1527两堆博弈模型,威佐夫博弈 #include<iostream> #include<cstdio> #include<queue> #include&l ...

  2. 方法的重写(override)两同两小一大原则:

    方法名相同,参数类型相同 子类返回类型小于等于父类方法返回类型, 子类抛出异常小于等于父类方法抛出异常, 子类访问权限大于等于父类方法访问权限.

  3. 关于MySQL IN LIKE OR使用索引的问题

    以前在网上看了一些资料,有些人说话不严谨,导致一直被误导,最近在实际开发中发现一些结论有问题,因此特地整理了一下,防止下次继续犯错. 以下前提是有对这个字段建立索引(简直废话,没建的肯定不会使用索引啊 ...

  4. python基础---递归函数真题解析

    方法一.有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中. 即: {'k ...

  5. FamilyFilter(4)

    3.1. 基于列族过滤数据的FamilyFilter 构造函数: FamilyFilter(CompareFilter.CompareOp familyCompareOp, ByteArrayComp ...

  6. Django项目:CRM(客户关系管理系统)--44--36PerfectCRM实现King_admin密码修改

    # king_urls.py # ————————02PerfectCRM创建ADMIN页面———————— from django.conf.urls import url from king_ad ...

  7. angular4 自定义表单组件

    自定义表单组件分为单值组件和多值组件. 单值组件:input/select/radio/textarea 多值组件:checkbox/tree组件 条件: 1.必须实现ControlValueAcce ...

  8. Redis之高可用、集群、云平台搭建

    原文:Redis之高可用.集群.云平台搭建 文章大纲 一.基础知识学习二.Redis常见的几种架构及优缺点总结三.Redis之Redis Sentinel(哨兵)实战四.Redis之Redis Clu ...

  9. day38 03-Spring的IOC和DI的区别

    在IOC中有一个DI的概念. IOC是控制反转,DI是依赖注入.现在编写的类里面是没有其他的属性的.如果你学过像UML设计的话, 电视没有遥控器,按按钮也可以,但是紧密的那种,像人和四肢,人如果没有了 ...

  10. day36 09-Hibernate中的事务:事务处理

    什么是丢失更新呢?有一条数据你查出来了,查出来之后有一个事务A和一个事务B也都过来操作同一个记录.A拿到这条记录之后可能改它的某一个值了,B拿到这条记录之后改它的另外一个值.这个时候A事务提交已经提交 ...