181. Flip Bits【easy】
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.
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】的更多相关文章
- 181. Flip Bits【LintCode, by java】
Description Determine the number of bits required to flip if you want to convert integer n to intege ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
随机推荐
- 【枚举约数】HackerRank - Week of Code 26 - Satisfactory Pairs
题意:给你一个正整数n,问你存在多少个正整数对a,b(a<b),满足条件:存在正整数x,y,使得ax+by=n. 就预处理出n以内所有数的约数,然后暴力枚举a,暴力枚举x,然后枚举n-ax的所有 ...
- 【R笔记】apply函数族
(1) apply apply函数通过对数组,矩阵,或非空维数值的数据框的“边缘”(margin)即行或列运用函数.返回值为向量,数组或列表. 函数形式 apply(X, MARGIN, ...
- Spring IOC 中三种注入方式
项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...
- Xcode升级后导致插件不能用, 一句代码更新UUID OK~
find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | ...
- SecureCRT实现跳板机自动登录
背景: 1.通常运维会开放几个内网的机器能跳转到外网机器进行访问,这样的就是跳板机. 2.比如线上有120.0.0.2这台机器,而内网192.168.1.2这台连接了VPN,能通过SSH登录120.0 ...
- 【java】JDK1.8时间日期库 新特性 所有java中时间Date的使用
除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学习如何使用java 8的这套API.Java对日期, ...
- UITextField 如何设置为密码方式显示?
UITextField 怎么设置成为一个 *号密码框 呢? 可以在 Interface Builder 里面直接设置吗? Attributes inspector 中 Text Field 下选中 S ...
- Virtualbox环境中安装Oracle 11gr2 RAC(ASM)
系统Oracle Linux 6.5,Oracle 11.2.0.1 终于开始安装ASM和RAC的行程了.开始前需要想清楚的几个事情: 如何规划网络配置(配置多网卡,实现连通性,规划内外网,eth0, ...
- PHP文件包含漏洞总结
0x00 前言 PHP文件包含漏洞的产生原因是在通过PHP的函数引入文件时,由于传入的文件名没有经过合理的校验,从而操作了预想之外的文件,就可能导致意外的文件泄露甚至恶意的代码注入. 最常见的就属于本 ...
- renderdoc on android
国内没人发这种贴...一个发了renderdoc with unity是在pc平台跑的 没有挂android 这货有点坑啊 花了好几个小时 wiki上的issue基本全看了...感觉是版本提交的log ...