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 ...
随机推荐
- 【转载】JSP生成静态Html页面
在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...
- 一些基于jQuery开发的控件
基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...
- BZOJ2186 SDOI2008沙拉公主的困惑(数论)
由于n!是m!的倍数,而对于每个与m!互质且小于m!的数x,x+m!.x+2*m!……也与其互质,所以答案即为(n!/m!)*φ(m!). φ(m!)=m!*∏(1-1/pi).其中的pi即为1~m中 ...
- vs2017自动生成的#include“stdafx.h”详解及解决方案
vs2017自动生成的#include“stdafx.h”详解及解决方案 问题描述: 在高版本的Visual Studio的默认设置中,会出现这么一个现象,在新建项目之后,项目会自动生成#includ ...
- Zju1610 Count the Colors
题面: 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目. Input: 每组测试数据第一行只有一个整数n, 1 <= n < ...
- 【题解】HAOI2018染色
好坑啊不开心…… 其实这题的想法还是比较简单粗暴的.题目明示恰好xxx,显然排除斜率二分这个玩意儿,那么不就只剩下容斥了嘛…… 令 \(A_{x}\) 为恰好出现了 \(S\) 次的至少有 \(x\) ...
- 【模考】2018.04.08 Travel
Description 有N个人出去旅行,第i个人去A国有Ai种游玩方式,去B国有Bi种游玩方式,问至少有C个人去A国的情况下,所有人的游玩方式有多少种不同的可能. 两种所有人的游玩方式不同当且仅当存 ...
- 【刷题】BZOJ 4998 星球联盟
Description 在遥远的S星系中一共有N个星球,编号为1-N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太 ...
- javascript和php使用ajax通信传递JSON
JS和PHP直接通信常用ajax完成,以实现js上UI的动态变化.通信使用JSON或者XML传递数据.下面详细描述两者直接JSON字符串的传递. 下面案例是要传递这样的json数据: { " ...
- Android Studio 创建/打开项目时一直处于Building“project name”Gradle project info 的解决
最近发现新版的AS,IDEA毛病不断,而且gradle的更新又给墙了,无奈啊! 进入类似如下的目录,发现如果没有对应的gradle解压文件,则在gradle官网下载完整压缩包,放入类似55xxxx串号 ...