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 ...
随机推荐
- Keil MDK中单个c文件生成LIB文件
看大多数说的都是简单地将整个工程转换成.LIB,在Project->Options for Target->Output下,选择Create Library,就可以了. 不过这样生成的li ...
- pyCharm最新2019激活码
破解补丁激活优点:到期时间为2099年,基本为永久啦 缺点:相对服务器激活麻烦些,但是一共只需要3个步骤,其实并不麻烦 一.下载 https://pan.baidu.com/s/1mcQM8CLUnw ...
- ava8并发教程:Threads和Executors
原文地址 原文作者:Benjamin Winterberg 译者:张坤 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教 ...
- Getting logback and slf4j to work in JBoss AS 7
As usual, it has to do with classloading and that JBoss internally also uses slf4j and logback. As e ...
- 【51Nod1773】A国的贸易 解题报告
[51Nod1773]A国的贸易 Description 给出一个长度为 \(2^n\) 的序列,编号从\(0\)开始.每次操作后,如果 \(i\) 与 \(j\) 的二进制表示只差一位则第 \(i\ ...
- 【poj1743】 Musical Theme
http://poj.org/problem?id=1743 (题目链接) 题意 给出n个音符的乐谱,求其中不重叠的重复最长连续段,重复连续段的定义为两段音符起伏相同. Solution 论文题,相邻 ...
- Java EE之表达式语言EL(下)
1.在EL表达式中使用作用域变量 表达式语言对作用域变量的支持,以及它解析变量的方式都使它变得非常有用. 1.1 EL表达式的隐式变量 EL表达式的作用域中定义了11个隐式变量. 当EL表达式引用了一 ...
- DataTables实现rowspan思路
直接看例子吧 <table id="example" class="display table table-bordered" cellspacing=& ...
- 简单版AC自动机
简单版\(AC\)自动机 学之前听别人说起一直以为很难,今天学了简单版的\(AC\)自动机,感觉海星,只要理解了\(KMP\)一切都好说. 前置知识:\(KMP\)(有链接) 前置知识:\(Trie\ ...
- 1:IMEI、MSISDN字段隐藏方法
1.在服务器上找到带有手机号码及IMEI的报表,下载到本地: 2.找到如下方框的表格列(例如:ID:104的报表):