题目:

如果要将整数A转换为B,需要改变多少个bit位?

样例

如把31转换为14,需要改变2个bit位。

()10=()2

()10=()2

挑战

你能想出几种方法?

解题:

A-->B二进制要变化多少位?就是考虑A、B对应的二进制数有多少不同的,A、B的异或结果中出现的1的个数就是需要改变的比特位

问题转换成,10进制的数中,二进制表示情况下1的个数,这个题目之前求过,编程之美上面也有的。

上面的方法1不可取,因为这里的数据有负数。

其他两种方法如下:

Java程序:

利用位运算:

class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int c = a^b;
int count = 0;
while(c!=0){
count += c&1;
c=c>>>1;
}
return count;
}
};

总耗时: 2834 ms

利用位运算 与减法

class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int c = a^b;
int count = 0;
while(c!=0){
c = c&(c-1);
count++;
}
return count;
}
};

总耗时: 2366 ms

取一位判断一位:

class Solution {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int count=0;
while(a!=0 || b!=0){
int ai = a&1;
int bi = b&1;
if(ai==1 && bi==0 || ai==0 && bi==1)
count++;
a = a>>>1;
b = b>>>1;
}
return count;
}
};

总耗时: 2103 ms

Python程序:

直接转换成python时间超时。。。

lintcode:Flip Bits 将整数A转换为B的更多相关文章

  1. Lintcode: Flip Bits

    Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...

  2. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  3. 位运算练习:将整数A转换为B,需要改变多少个bit位

    思路解析: 将整数A转换为B,如果A和B在第i(0<=i<32)个位上相等,则不需要改变这个BIT位,如果在第i位上不相等,则需要改变这个BIT位.所以问题转化为了A和B有多少个BIT位不 ...

  4. 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".

    题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...

  5. lintcode-181-将整数A转换为B

    181-将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 注意事项 Both n and m are 32-bit integers. 样例 如把31转换为14,需要改变2个bit位 ...

  6. 181. Flip Bits【easy】

    181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n  ...

  7. 181. 将整数A转换为B

    181. 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 注意事项 Both n and m are 32-bit integers. 您在真实的面试中是否遇到过这个题? Yes ...

  8. LintCode_181 将整数A转换为B

    题目 如果要将整数A转换为B,需要改变多少个bit位? 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 思路 要考虑负数的问题 如果 一正一负 将他们去全部变成正数 后 ...

  9. 181. Flip Bits【LintCode, by java】

    Description Determine the number of bits required to flip if you want to convert integer n to intege ...

随机推荐

  1. 在指定的DSN中,驱动程序和应用程序之间的体系结构不匹配

    今天在使用plsql通过odbc导入excel数据时发生了一个错误,截图如下: 错误提示为:驱动程序和应用程序之间的体系结构不匹配. 后来百度了一下,得出答案.系统是win10 64位.excel驱动 ...

  2. WinForm TreeView节点重绘,失去焦点的高亮显示

    当用户焦点离开TreeView时,TreeView选中节点仍然高亮,但是颜色符合主题. 设置TreeView.HideSelection = False;可让选中节点保持高亮. 添加重绘事件 Tree ...

  3. 【Qt】Qt之重启应用程序【转】

    简介 今天分享的内容有些意思-如何重启一个应用程序.其实,有时候这是一个很重要的功能点,而且很人性化.易用性很好. 例如:切换用户.当某个用户登录成功之后,需要切换到其它账号,那么这时,你就知道它的重 ...

  4. PHP连接SQL Server(sqlsrv)

    配置好php环境后,下载如下依赖包,解压目录选择php的ext目录,并在php.ini中加上(我的php版本为5.6) extension=php_pdo_sqlsrv_56_ts.dll exten ...

  5. hibernate 知识梳理

    一.hibernate背景介绍: 作者: Gavin King 分hibernate ORM(for relation db),OGM(for nosql db),hearch,validator,t ...

  6. PHP操作MongoDB简明教程(转)

    转自:http://blog.sina.com.cn/s/blog_6324c2380100ux2m.html MongoDB是最近比较流行的NoSQL数据库,网络上关于PHP操作MongoDB的资料 ...

  7. js学习笔记一类型、值和变量

    js的数据类型分为两类:原始类型和对象类型 原始类型包括数字.字符串和布尔值 js中有2个特殊的原始值:null(空)和undefined(未定义) 对象是属性的集合,每个属性都由名/值对组成 js的 ...

  8. hibernate知识点理解

    1.只有业务逻辑层出现的问题? 1.切换数据库麻烦 2.sql编写起来麻烦 3.我们的程序员不需要关注数据库,只希望关心业务本身 2.hibernate的好处 1.程序员只关心业务逻辑,使角色更加清楚 ...

  9. [转]MAC下JDK版本的切换

    系统里之前先安装里jdk6的,后台又装里7,安装完成后,java -version 版本是7,  导致我eclipse打不开,一开始的做法是,把7的版本给删除掉. 删除的方法也很简单,在命令行中到 / ...

  10. sqlldr使用

    一.写ctl文件 首先,先写一个ctl文件(包含控件信息的文件,这里是oracle数据库的控制文件)文件名:测试.ctl ctl文件例: load datainfile 'd:\xxx.cvs'tru ...