题目:

如果要将整数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. AngularJS(14)-动画

    AngularJS 提供了动画效果,可以配合 CSS 使用. AngularJS 使用动画需要引入 angular-animate.min.js 库. <!DOCTYPE html> &l ...

  2. phpStudy 2016 更新下载,新版支持php7.0

    目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 『软件简介』该程序包集成 ...

  3. mongodb 入门笔记

    选择Mongo的关键是:这是一个 JSON 文档数据库. 1. Mongo 的术语 文档:一条完整的数据就是一个文档(对应于 MySQL 的一行). 集合:一组文档构成一个集合.类似 MySQL 中表 ...

  4. DirectoryEntry配置IIS出现ADSI Error:未知错误(0x80005000)

    目录 问题案例 原因分析 解决问题 总结 问题案例 DirectoryEntry配置IIS,在IIS6.0下运转正常,但IIS7.0下运转会出错: System.DirectoryServices.D ...

  5. Spark菜鸟学习营Day5 分布式程序开发

    Spark菜鸟学习营Day5 分布式程序开发 这一章会和我们前面进行的需求分析进行呼应,完成程序的开发. 开发步骤 分布式系统开发是一个复杂的过程,对于复杂过程,我们需要分解为简单步骤的组合. 针对每 ...

  6. MySQL监控工具-orzdba

    源代码地址:http://code.taobao.org/p/orzdba/src/trunk/     [root@hank-yoon servers]# chmod +x orzdba 在代码的1 ...

  7. java移位操作符

    <<:左移操作符,右边补0,相当于乘二乘二... >>:右移操作符,左边补符号位(正数补0,负数补1),相当于除二除二... >>>:无符号右移,左边补0,相 ...

  8. javascript之流程控制 和函数的容易忽略点

    1.流程控制 1> for in  仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...

  9. Error: Cannot find module 'express'

    安装Express命令如下: npm install -g express 安装成功之后会在C:\Users\[YOUR_USER_NAME]\AppData\Roaming\npm\node_mod ...

  10. 好项目烂架构的问题,四年coder的吐槽

    四年多码农,毕业后在一家小私企做前端:(初始asp.net,对oo有了比较深切的理解:处于对某空间的效仿,对前端技术架构理解的比较透彻): 在这家公司混了4个月之后跳出来想自己单干: 自己接了个小项目 ...