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. 第28章 LTDC—液晶显示中英文

    本章参考资料:<STM32F76xxx参考手册>.<STM32F7xx规格书>.库帮助文档<STM32F779xx_User_Manual.chm>. 关于开发板配 ...

  2. Vue教程:过滤器filters(五)

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaScrip ...

  3. OSTimeDlyHMSM函数

    1.os_time.c里面 #if OS_TIME_DLY_HMSM_EN > 0 INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U ...

  4. 自动化运维工具Ansible实战(四)常用模块

    转载链接:http://blog.51cto.com/liqingbiao/1962609   Ansible模块按功能分为:云模块.集群模块. 命令模块.数据库模块.文件模块.资产模块.消息模块.监 ...

  5. 关于Hibernate的部分知识总结

    [部分内容参考地址:https://www.cnblogs.com/woniu2219/p/7111857.html] Hibernate Hibernate是一个开放源代码的对象关系映射框架,它对J ...

  6. 转:Java中的cas

    引自:https://blog.csdn.net/mmoren/article/details/79185862 本篇的思路是先阐明无锁执行者CAS的核心算法原理然后分析Java执行CAS的实践者Un ...

  7. 壹度DIY_微信小程序组件_小程序插件开发

    开源免费插件,diy特有的页面机制,搭配30+自定义组件,让你的站点每一个页面都可以完全自定义,可无缝对接任意小程序,如有疑问加入qq壹度小程序交流群:302866773:或wx:liu2417301 ...

  8. 虚拟机搭建hadoop的步骤

    1.首先是安装Vmware Workstation,下载地址:https://www.vmware.com/products/workstation-player/workstation-player ...

  9. linux线程篇 (三) 线程的同步

    1 互斥量 pthreat_mutex_t mymutex; //1. 创建 初始化 int pthread_mutex_init(pthread_mutex_t *mutex, const pthr ...

  10. 03以太网帧结构(链路层 IEEE802.3)

    OSI七层模型:从底往上记(研究细致时用) 物理层:单位bit,字节byte,同轴电缆,光纤,二进制,比特流 数据链路层:帧,16进制,0-9,A-FMac地址->全网唯一性     mac地址 ...