题目:

如果要将整数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. python 内置模块之logging

    1.将日志直接输出到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messag ...

  2. 如何参与linux 内核开发

    如果想评论或更新本文的内容,请直接联系原文档的维护者.如果你使用英文 交流有困难的话,也可以向中文版维护者求助.如果本翻译更新不及时或者翻 译存在问题,请联系中文版维护者.   英文版维护者: Gre ...

  3. linux内核中的get_user和put_user

    linux内核中的get_user和put_user 在 内核空间和用户空间交换数据时,get_user和put_user是两个两用的函数.相对于copy_to_user和 copy_from_use ...

  4. [转载]iOS面试题总

    转载自:http://blog.sina.com.cn/s/blog_67eb608b0101r6xb.html (2014-06-13 20:23:33) 转载▼ 标签: 转载   crash 原文 ...

  5. 十一、 BOOL类型、分支结构和关系运算符

    BOOL类型:表示非真即假.只有两个值:YES和NO,而二进制只识别二进制数,所以,将YES替换为“1”,NO替换为“0” BOOL数据类型占一字节的空间内存 BOOL数据类型输出为:%lu:输入为: ...

  6. iOS优化内存方法推荐

    1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...

  7. 升级iOS10之后调用摄像头/麦克风等硬件程序崩溃闪退的问题

    在升级到iOS10之后, 开发过程中难免会遇到很多的坑, 下面是一些常见的坑, 我做了一些整理, 希望对大家开发有帮助: &1. 调用视频,摄像头, 麦克风,等硬件程序崩溃闪退的问题: 要注意 ...

  8. 小王子浅读Effective javascript(一)了解javascript版本

    哈哈,各位园友新年快乐!愚安好久没在园子里写东西了,这次决定针对javascript做一个系列,叫做<小王子浅读Effective javascript>,主要是按照David Herma ...

  9. android 开启或者隐藏软键盘

    一. 隐藏软键盘方法一(注:此方法本人使用时发现isActivie()失效,建议还是用其他方法..): InputMethodManager imm = (InputMethodManager)get ...

  10. Android-Empty-Layout:展示不同类型的页面布局,用于视图是空的时候

    Android-Empty-Layout:这个布局可以作用在Listview,Gridview,用于显示数据的是空的时候,可以提示友好的页面.这库可以显示页面出错,页面加载,页面是空. 加载的动画页面 ...