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 ...
随机推荐
- 冒泡排序--注意flag变量的设置
代码: #include<stdio.h> void BubbleSort(int a[],int n){ int i,j; int temp; ; // 此处flag变量的设置可以提高算 ...
- 关于数字、数据处理的几个PHP函数汇总
1. / 得到的结果是浮点数 2. % 求余数 3.ceil():得到大于当前数字的整数 $num=3.4; $num=ceil($num); echo $num; 的到的结果是4 $num=3. ...
- Vue 单页应用:记事本
若文章中存在内容无法加载的情况,请移步作者其他博客. 简书 CSDN 最近在看 Vue 的时候,别人给我安利了一个国外的小案例,通过 Vue 和 Vuex 来实现一个记事本. 仔细剖析下,发现“麻雀虽 ...
- WebGL可视化地球和地图引擎:Cesium.js
http://www.open-open.com/lib/view/open1427341416418.html Cesium 是一个JavaScript 库用于在Web浏览器创建 3D 地球和 ...
- JS组件系列——自己封装一个上传文件组件
页面调用: $('#fileUpload').cemsUpload({ errorEmpty:'<s:text name="cupgrade.view.tip.upload.file. ...
- HDFS 中向 DataNode 写入数据失败了怎么办
https://blog.csdn.net/HeatDeath/article/details/79012258 http://wenda.chinahadoop.cn/question/3323 h ...
- 2017.12.07 postgresql使用with recursive完成迭代查询
1.表结构 2.需求 查询某条记录的所有父亲节点,或者所有孩子节点. 3.向上查询(查询所有父亲节点) 注意,这里返回的记录包含自己. sql如下: WITH RECURSIVE res AS ( S ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- mycat读写分离与主从切换
1, 分库分表的优缺点.以及为什么分表方式无法成为主流? 分表:在台server上,长处是易维护,相似表分区.缺点是在一台dbserver上.无法分担IO.负载集中. 分库:在多台server上,长处 ...
- non-compatible bean definition of same name and class
在整合struts2.1.6+spring2.5.6开发中,使用了注解和struts-convention来实现零配置管理.spring也使用注解annotation方式.现在的问题是:我在连个个不同 ...