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 ...
随机推荐
- 【二维树状数组】bzoj1452 [JSOI2009]Count
权值的种类只有100种,对每种开个二维BIT,然后是经典操作. #include<cstdio> using namespace std; ][]; struct BIT_2D { ][] ...
- 【最小瓶颈生成树】【最小生成树】【kruscal】bzoj1083 [SCOI2005]繁忙的都市
本意是求最小瓶颈生成树,但是我们可以证明:最小生成树也是最小瓶颈生成树(其实我不会).数据范围很小,暴力kruscal即可. #include<cstdio> #include<al ...
- spring boot 2.x静态资源会被HandlerInterceptor拦截的原因和解决方法
在spring boot 1.5.x中,resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,比如静态资源放置位置如下图所示: 那么访问静态资源的路径可以是: ...
- 阿里云(ECS)Linux客户端SSH会话连接超时OperationTimedOut
问题描述:使用SecureCRT等SSH客户端连接Linux服务器时,提示Operation timed out. 问题原因:SSH服务未配置或注释掉向SSH客户端连接会话发送频率和时间. 解决方法: ...
- O2S.Components.PDFRender4NET.dll 图片空白BUG问题
在用网上的2.0.1.0破解版的时候,会出现莫名的错误,大多数pdf转成图片都是Ok的,但是有部分转出来是空白,因为我这里要涉及大量的pdf转图片,不允许出现错误. 后来经过实验发现是这个版本存在bu ...
- 【SQL Server】sql server更改了数据表的字段/新增数据表的字段 无法保存
sql server更改了数据表的字段/新增数据表的字段 无法保存 解决方法:进入 工具-->选项-->Designers-->表设计器和数据库设计器-->取消勾选 即可
- 【java】乱码处理+编码转化+判断字符串编码方式
之前有一篇是修改IDE的编码,服务器的编码等处理乱码,但是在所有环境因素上,保证了编码方式之后,也会有前台传递给后台[get方式提交]传递给后台的编码方式是非UTF-8的,也会有例如FTP服务器的编码 ...
- css字体font-family
1."Arial" 2."Microsoft YaHei" 3."黑体" 4."宋体" 5.sans-serif 6.T ...
- 一个简单的假vue全家桶(vue+vue-router+require)
首先说明我觉得这是一个比较好理解的vue全家桶(虽然是假的),模块化也是用require来做的,而且如果后期有必要压缩我也会用gulp来做 1.依赖个个本地模块,require只是用来载入page,这 ...
- jquery给input标签添加data-options属性
//原生JS实现document.getElementById('startPrice').setAttribute("data-options", "required: ...