Flip Bits
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 {
/**
*@param a, b: Two integer
*return: An integer
*/
public static int bitSwapRequired(int a, int b) {
// write your code here
int diff = a ^ b;
int count = ;
while (diff != ) {
count++;
// remove the last 1
diff = diff & (diff - ); // 这种方法很好。
}
return count;
}
};
Flip Bits的更多相关文章
- 181. Flip Bits【easy】
181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n ...
- LintCode刷题笔记--Flip Bits
Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n ...
- Lintcode: Flip Bits
Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...
- 181. Flip Bits【LintCode, by java】
Description Determine the number of bits required to flip if you want to convert integer n to intege ...
- lintcode:Flip Bits 将整数A转换为B
题目: 将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 样例 如把31转换为14,需要改变2个bit位. ()10=()2 ()10=()2 挑战 你能想出几种方法? 解题: A- ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- c++ bitset使用
A bitset is a special container class that is designed to store bits (elements with only two possibl ...
- 黑科技--位集--bitset
自从上次网赛发现这么个东西之后,深深地感受到了bitset的强大,0.0. 正常的bool占用1字节空间,bitset可以把这个缩到1bit,空间上8倍优化.正常用起来可能会跟位运算状态压缩类似,但是 ...
- C++ std::vector<bool>
std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...
随机推荐
- Atcoder arc080E Young Maids(线段树+优先队列)
给出一个n排列,每次可以选择相邻的两个数字放在新的排列首部,问最后形成的新的排列字典序最小是? 考虑新排列的第一个数字,则应是下标为奇数的最小数,下标不妨设为i.第二个数字应该下标大于i且为偶数的最小 ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
- 【BZOJ4006】管道连接(动态规划,斯坦纳树)
题面 BZOJ 洛谷 题解 和这题区别不是很大吧. 基本上拿过来改一下就做完了. #include<iostream> #include<cstdio> #include< ...
- HDFS问题集(一),使用命令报错:com.google.protobuf.ServiceException:java.lang.OutOfMemoryError:java heap space
仅个人实践所得,若有不正确的地方,欢迎交流! 一.起因 执行以下两条基本的HDFS命令时报错 hdfs dfs -get /home/mr/data/* ./ hdfs dfs -ls /home/m ...
- 【bzoj2876】 Noi2012—骑行川藏
http://www.lydsy.com/JudgeOnline/problem.php?id=2876 (题目链接) 题意 在满足约束条件$${\sum_{i=1}^ns_ik_i(v_i-v_i' ...
- 20135239 Linux内核分析 期中总结
期中总结 链接 网易云课堂 MOOC 摘录与实验内容 第一周: http://www.cnblogs.com/20135239-yxlm/p/5216842.html 第二周: http://www. ...
- mysqlbinlog- 处理二进制日志文件的实用工具 学习笔记
参考 MySQL 5.5官方简体中文参考手册完美版 8.6 节 调用: shell> mysqlbinlog [option] log-files... mysqlbinlog支持下面选项: ...
- 解题:NOI 2014 购票
题面 观察一下部分分,我们发现链上的部分分是这样一个DP: $dp[i]=min(dp[i],dp[j]+dis(i,j)*p[i]+q[i])(dis(i,j)<=lim[i]\&\& ...
- 解题:HEOI 2013 SAO
题面 不好讲,直接上式子吧=.= 设$dp[i][j]$表示考虑完$i$的子树后$i$的排名为$j$的方案数,然后转移类似树形背包,具体来说是(这里假设子树在$i$后选,其实反过来还用这个式子答案也是 ...
- 解题:CEOI 2017 Mousetrap
题外话: 这是制杖yd的交流题目 题面 首先把捕鼠夹所在的点提出来当根,然后这变成了一棵有根树,我们先来看耗子移动的影响 可以发现耗子往下走就回不来了,而且最后还会被困在一个叶子上,那么这个时候我们把 ...