5.5 Write a function to determine the number of bits required to convert integer A to integer B.
EXAMPLE
Input: 31,14
Output: 2

这道题给了我们两个数字A和B,问如果将A转化为B需要变几个位,那么我们很容易想到要用异或来做,因为相同位异或为0,那么为1的为就是不相同的位,总和就是我们要求的结果。那么此题就转化为求异或结果中位‘1’的个数,我们可以用for循环来做,判断异或数的最低位是否为1,若为1就计数器加1,然后将异或数向右移动一位,以此类推直至异或数为0,参见代码如下:

解法一:

class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c >>= ) {
res += c & ;
}
return res;
}
};

还有一种方法,这里不是将异或数每次向右移动一位,而是用n&(n-1)来清楚最低有效位Least Significant Bit,每清除一个,计数器增1,直到异或数为0停止,参见代码如下:

解法二:

class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c &= c - ) {
++res;
}
return res;
}
};

Tips:根据之前那篇Single Number III 单独的数字之三,我们知道得到最低有效位Least Significant Bit的方法是n&(-n),而这道题又学习了清除最低有效位Least Significant Bit的方法是n&(n-1),这些小技巧需要我们深刻理解,并在今后的解题中灵活的使用,这才是刷题的精髓所在。。。

[CareerCup] 5.5 Number of Converted Bits 转换数字所需的位数的更多相关文章

  1. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  2. LeetCode 191:number of one bits

    题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  3. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  4. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  6. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  7. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  8. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  9. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

随机推荐

  1. sql 存储过程中top 后面跟参数的问题

    之前存储过程中有top的情况,都是拼接sql,然后通过exec执行,进行查询结果,很不方便. 今天研究了,原来top后面是可以直接写参数的. 只需要top 后面的参数加上小括号就好了 eg: TOP ...

  2. 手动删除webapps下项目,导致Document base %TOMCAT_HOME%\webapps\XXX does not exist or is not a readable directory

    删除 %TOMCAT_HOME%\conf\XXX.xml , 再次eclipse中重新启动tomcat,错误就会消失.

  3. oracle-删除归档日志

    rman target user/password; DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7';

  4. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. php写守护进程(Daemon)

    守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种很有用的进程.php也可以实现守护进程的功能. 1.基本概念 进程 ...

  6. gdb 远程调试 android native 程序

    ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 先看一张原理图: 我是 Linux 和 Android 双料 0 基础,目前对 gdb 了解的很浅显.(注意 ...

  7. FileInputFormat

    MapReduce框架要处理数据的文件类型 FileInputFormat这个类决定. TextInputFormat是框架默认的文件类型,可以处理Text文件类型,如果你要处理的文件类型不是Text ...

  8. NPOI 读取excel到DataTable 读取隐藏列 读取公式列

    处理思路: 1.打开excel 用NPOI进行读取: 2.读取第一个Sheet: 读取过程中: a.先设置相应列 不隐藏 b.读取Cell时 先判断是否的包含公式 相应代码如下: public sta ...

  9. 【读书笔记《Android游戏编程之从零开始》】15.游戏开发基础(剪切区域)

    剪切区域也称为可视区域,是由画布进行设置的:它指的是在画布上设置一块区域,当画布一旦设置了可视区域,那么除此区域外,绘制的任何内容都将看不到:可视区域可以是圆形.矩形等等. 画布提供了三种设置可视区域 ...

  10. React 之 Hello world

    一入react深似海,从此学习为常态,react 成为了一种趋势,很多人应该很多人准备进坑,下面对react进行简单的描述: 首先学习react,要有多方学习的准备,例如:Webpack, Babel ...