题目:

如果要将整数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. 用jQuery解析复杂的xml结构文件

    一个晚上的心血 <?xml version="1.0" encoding="UTF-8"?> <weibo><wbContent& ...

  2. Grunt 构建SeaJS

    GitHub地址:https://github.com/MrLeo/SeaJS 目录结构 目录结构说明 web存放HTML文件 static存放所有HTML需要用到静态资源文件(css.js.img- ...

  3. PHP运行方式对比

    文章内容来自以下站点http://www.cnblogs.com/xia520pi/p/3914964.html 关于PHP目前比较常见的五大运行模式: 1.CGI(通用网关接口 / Common G ...

  4. WinForm 实现验证码

    private void CheckIdentifyingCode() { Random r = new Random(); string str = ""; for (int i ...

  5. PHP CodeIgniter(CI)去掉 index.php

    去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...

  6. Dataguard之redo传输服务

    一.Data Guard架构 整个Data Guard体系就是围绕三个关键点展开: 日志发送(Redo Send) 日志接收(Redo Receive) 日志应用(Redo Apply) 二.日志发送 ...

  7. oracle11g关于表空间的问题

    1.oracle11g默认的块大小为8K  每个表空间里面的单个数据文件最大为32G   (2^22-1) *4k   最多可以放1024个单个文件    SQL> show parameter ...

  8. MySQL 简洁连接数据库方式

    OS  :   CentOS 6.3 DB  :  5.5.14 MySQL连接数据库的方式很多: 1.[root@db01 bin]# ./mysql -uroot -p 2.[root@db01 ...

  9. kendo ui template的用法

    kendo ui template的用法: Kendo UI 框架提供了一个易用,高性能的JavaScript模板引擎.通过模板可以创建一个HTML片段然后可以和JavaScript数据合并成最终的H ...

  10. 部署图 Deployment Diagram

    UML部署图描述了一个运行时的硬件结点,以及在这些结点上运行的软件组件的静态视图. 部署图显示了系统的硬件,安装在硬件上的软件,以及用于连接异构的机器之间的中间件. 下面这张图介绍了部署图的基本内容: ...