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 ...
随机推荐
- MyFlash闪回恢复数据
使用限制: .binlog格式必须为row,且binlog_row_image=full. .仅支持5.6与5.. .只能回滚DML(增.删.改). .mysqlbinlog版本请保持一致. 1.安装 ...
- BZOJ5125 小Q的书架(决策单调性+动态规划+分治+树状数组)
设f[i][j]为前i个划成j段的最小代价,枚举上个划分点转移.容易想到这个dp有决策单调性,感性证明一下比较显然.如果用单调栈维护决策就不太能快速的求出逆序对个数了,改为使用分治,移动端点时树状数组 ...
- [NOIP2011]玛雅游戏
闲的没事干,出来写一下早两天刷的一道搜索题NOIP2011玛雅游戏,其实这道题还是比较水的,虽然看起来可能有点复杂. 方法很简单粗暴,直接根据规则模拟就行. 话不多说直接上代码(关键操作在注释中有提到 ...
- Kotlin入门简介
Kotlin的“简历” 来自于著名的IDE IntelliJ IDEA(Android Studio基于此开发) 软件开发公司 JetBrains(位于东欧捷克) 起源来自JetBrains的圣彼得堡 ...
- 一、spark错误
1. 17/07/17 15:34:55 ERROR yarn.ApplicationMaster: User class threw exception: java.lang.Unsupported ...
- getopt_long
http://blog.csdn.net/lanyan822/article/details/7692013 在程序中难免需要使用命令行选项,可以选择自己解析命令行选项,但是有现成的,何必再造轮子.下 ...
- Python【zip-map-filter】三个内置函数
print("============内置函数:zip===========")l2 = ['a','b','c','e','f','g']l3 = [1,2,3]L4=['A', ...
- Ansible5:常用模块
目录 ping模块 setup模块 file模块 copy模块 service模块 cron模块 yum模块 user模块与group模块 user模块 group示例 synchronize模块 f ...
- 转:SkipList跳表
http://kenby.iteye.com/blog/1187303 相关概念: 1.几何分布 http://baike.baidu.com/link?url=DdtNq6pCWIvr7onVBtE ...
- 在MySQL或者SQLServer中,添加对象后自动返回主键到对象模型中的配置方式
设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中.MySQL和SQLServer执行auto-generated key fiel ...