Description

Determine the number of bits required to flip if you want to convert integer n to integer m.

Both n and m are 32-bit integers.

Example

Given n = 31 (11111), m = 14 (01110), return 2.

解题:比较两个整数对应的二进制数,共有多少位不同。注意,负数也包含在内。“>>>”在无符号右移,用0补齐。

 public class Solution {
/**
* @param a: An integer
* @param b: An integer
* @return: An integer
*/
public int bitSwapRequired(int a, int b) {
// write your code here
int count = 0;
for (int c = a ^ b; c != 0; c = c >>> 1) {
count += c & 1;
}
return count;
}
}

181. Flip Bits【LintCode, by java】的更多相关文章

  1. 181. Flip Bits【easy】

    181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n  ...

  2. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  3. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  4. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  5. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  6. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  7. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  8. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  9. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

随机推荐

  1. HDU 1273 漫步森林(数学 找规律)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. tomcat解析

    tomat是一个servlet容器,来处理http请求.在平时的使用中我们都会再浏览器中输入http地址来访问服务资源,比如格式http://host[":"port][abs_p ...

  3. Knowledge Point 20180305 Java程序员详述编码Unicode

    Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设 ...

  4. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  5. ubuntu系统的软件包管理工具

    ubuntu系统的软件包管理工具有两种,一种是离线管理,另一种是在线管理 1.离线管理 dpkg工具可以对本地存放的deb安装包进行安装,卸载,查看状态等. dpkg -i app_name_vers ...

  6. 【转】Red5流服务器搭建(实现在线直播,流媒体视频播放和在线视频会议)

    来自:http://blog.csdn.net/sunroyi666/article/details/52981639 一. 先介绍一下流媒体技术:所谓流媒体技术,是指将连续的影像和声音信息经过压缩处 ...

  7. python学习笔记:第5天 基本数据类型之字典

    目录 字典的介绍 补充 字典的介绍 字典(dict)是python中唯一的一个映射的数据类型,它是以一对一对的键值对的形式组成的一个容器,在python中字典也是可变的,我们也可以在字典中存放各种类型 ...

  8. PHP变量问题,Bugku变量1

    知识点:php正则表达式,php函数,全局变量GLOBALS(注意global和$GLOBALS[]的区别) PHP函数: isset():     条件判断 get方法传递的args参数是否存在 p ...

  9. C语言实例解析精粹学习笔记——18

    <C语言实例解析精粹>中编译环境采用的是Turbo C 2.0.但是这个编译器年代久远,较新的编译器对书中的某些例子支持不好,在学习的时候同时做一些笔记. 实例18:将一个无符号整数转换为 ...

  10. 【数据结构】线性表&&顺序表详解和代码实例

    喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 预备知识 1.0 什么是线性表? 线性表(List)是零个或者多个数据元素的有限序列. 首先它是一个序列.里面的元素是有顺 ...