181. Flip Bits【easy】

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

Notice

Both n and m are 32-bit integers.

Example

Given n = 31 (11111), m = 14 (01110), return 2.

解法一:

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

每次循环都会修改异或后的结果,循环退出条件就看该值是否为0即可,不用非要搞32位。

解法二:

 class Solution {
public:
/**
*@param a, b: Two integer
*return: An integer
*/ /*
int num_of_1(int a){
int num = 0;
while (a){
if (a >> 1){
num++;
}
a = a >> 1;
}
return num;
}
*/
//上面程序当a为负数时错误
int num_of_1(int a){
int num=; for(int i=;i<;i++){
if(a&(<<i)){
num++;
}
} return num;
} int bitSwapRequired(int a, int b) {
// write your code here
int XOR=a^b;
return num_of_1(XOR);
}
};

左移还是右移?对1左移可以避免对异或后的数右移带来的bug,或者对于异或后的结果仅仅右移32位也可以保证没有问题。

参考自:http://blog.csdn.net/gao1440156051/article/details/50590427

解法三:

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

参考自:http://blog.csdn.net/shinanhualiu/article/details/49003797

补充一下上面用到的位运算操作。

n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子:
n = 10100(二进制),则(n-1) = 10011 ==> n&(n-1) = 10000
可以看到原本最低位为1的那位变为0。
弄明白了n&(n-1)的作用,那它有哪些应用?

1、 判断一个数是否是2的方幂
n > 0 && ((n & (n - 1)) == 0 )

解释((n & (n-1)) == 0):

如果A&B==0,表示A与B的二进制形式没有在同一个位置都为1的时候。

那么本题到底啥意思??

不妨先看下n-1是什么意思。

令:   n=1101011000(二进制,十进制也一样),则

n-1=1101010111。

n&(n-1)=1101010000

由此可以得出,n和n-1的低位不一样,直到有个转折点,就是借位的那个点,从这个点开始的高位,n和n-1都一样,如果高位一样这就造成一个问题,就是n和n-1在相同的位上可能会有同一个1,从而使((n & (n-1)) != 0),如果想要

((n & (n-1)) == 0),则高位必须全为0,这样就没有相同的1。

所以n是2的幂或0

2. 求某一个数的二进制表示中1的个数,正如本题。

参考自:http://blog.csdn.net/navyifanr/article/details/19496459

181. Flip Bits【easy】的更多相关文章

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

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

  2. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  3. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  4. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  8. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  9. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

随机推荐

  1. [Contest20180321]nonintersect

    $\dfrac 2\pi$是个引人注目的数字,先来看看它到底是什么东西 假如有一条直线,它和题目所给的某条长度为$d$的线段夹角为$\theta$,那么线段在直线上的投影长度为$\left|d\cos ...

  2. openresty的ngx.timer.at

    openresty的ngx.timer.at真是个强大的方法. 例如某些函数不可以在一些NGINX的执行阶段使用时,可以ngx.timer.at API 创建一个零延迟的timer,在timer中去处 ...

  3. 1.5(java学习笔记)this关键字

    this关键字主要有三个作用 1.调用本类中的属性. public class TextThis { public static void main(String[] args){ Person p1 ...

  4. NDK 调用对象属性

    JNIEXPORT jbyteArrayJNICALL Java_com_lanhetech_iso8583_nativeLib_Iso8583NativeLib_pubPack8583 (JNIEn ...

  5. GNS3 思科模块 说明

    GNS3整合了如下的软件:   Dynamips :一款可以让用户直接运行Cisco系统(IOS)的模拟器   Dynagen :是Dynamips的文字显示前端   Pemu : PIX防火墙设备模 ...

  6. winform如何保持TreeView节点展开和折叠的状态

    转载:http://blog.sina.com.cn/s/blog_6abcacf5010138q5.html private Hashtable NodesStatus = new Hashtabl ...

  7. 用Qemu模拟vexpress-a9 (五) --- u-boot引导kernel,device tree的使用

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  8. OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决

    这问题根本不是DirectX问题,不知道网上怎么这么这样的回复.而且也不亲自验证一下.下面将自己花很多时间才解决的方式整理一下. 因为一般情况下你配置的OpenCV加入系统环境变量的都是X86下的bi ...

  9. Android2017最新面试题(3-5年经验个人面试经历)

    2017最新Android面试题 大家好,在跟大家讲述自己的面试经历,以及遇到的面试题前,先说说几句题外话. 接触Android已经3年,在工作中遇到疑难问题总是在网上(csdn大牛博客,stacko ...

  10. PostgreSQL配置文件--连接和认证

    1 连接和认证 CONNECTIONS AND AUTHENTICATION 1.1 连接 CONNECTIONS 1.1.1 listen_addresses 字符型 默认: listen_addr ...