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. 测试是否是移动端,是否是iphone,是否是安卓

    function isMobile(){ return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(na ...

  2. Python-购物车系统

    # coding=utf-8 import os, pickle class color: def echo_error(self, red): print(f"\033[31;1m {re ...

  3. PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么)

    PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么) 一.总结 一句话总结: 拿时间换空间:针对内存泄露的情况,可以节省大量的内存空间,但是由于垃圾回收算法运行耗费时间,开启垃圾回收算法会增加脚本 ...

  4. PHP declare 之 strict_types=1

    PHP中申明 declare(strict_types=1)的作用:  strict_types=1 及开启严格模式.默认是弱类型校验.具体严格模式和普通模式的区别见下面代码. code1: < ...

  5. 移动端iPhone系列适配问题

    问题一:苹果手机上的input按钮自带渐变效果 一样的代码,为啥在苹果手机上的input按钮就自带渐变效果,搞特殊吗?怎么让它显示正常?只需要加上outline:0px; -webkit-appear ...

  6. 解决 Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'

    If you are looking for the solution in Android Studio : Right click on your app Open Module Settings ...

  7. 2018-2019年中国CDN市场发展报告:阿里云成为中国CDN市场的领军者

    近日,权威ICT市场咨询机构计世资讯(CCW Research)发布<2018-2019年中国CDN市场发展报告>,报告显示,当前,随着新型信息技术在中国不断应用,以及互联网化新业务的快速 ...

  8. JavaScript中this的指向(转载)

    转载自:http://www.cnblogs.com/dongcanliang/p/7054176.html 前言 this 指向问题是入坑前端必须了解知识点,现在迎来了ES6时代,因为箭头函数的出现 ...

  9. [转]js的垃圾回收机制

    javascript具有自动垃圾收集机制,执行环境会负责管理代码执行过程中使用的内存.在编写javascript程序时,开发人员不用再关心内存使用问题,所需内存的分配以及无用内存的回收完全实现了自动管 ...

  10. 10分钟完成 mongodb replSet 部署

    开始: ------------------------------------------------------------------------------------------------ ...