Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.

分析:http://www.cnblogs.com/grandyang/p/4431646.html

我们先从题目中给的例子来分析,[5, 7]里共有三个数字,分别写出它们的二进制为:

101  110  111

相与后的结果为100,仔细观察我们可以得出,最后的数是该数字范围内所有的数的左边共同的部分,如果上面那个例子不太明显,我们再来看一个范围[26, 30],它们的二进制如下:

11010  11011  11100  11101  11110

 public class Solution {
int rangeBitwiseAnd(int m, int n) {
int i = ;
while (m != n) {
m >>= ;
n >>= ;
++i;
}
return (m << i);
}
}

Bitwise AND of Numbers Range的更多相关文章

  1. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  2. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  3. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  4. LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range

    1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...

  5. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

  6. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  7. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  8. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  9. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  10. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

随机推荐

  1. Ibatis学习总结1--ibatis简介和SQL Maps

    最佳维护的一个项目使的是ibatis框架,在闲暇之余将手头的开发手册和平时开发的理解做一下总结,言归正传. 简介 使用 SQL Map,能够大大减少访问关系数据库的代码.SQL Map 使用简单的 X ...

  2. 50行代码仿backbone_todos

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...

  4. Spring3.2.2之后不赞成使用queryForInt

    原来: public int getMatchCount(String username,String password){ String sql="select count(*) from ...

  5. STL Iterators

    Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...

  6. FileStream文件流的读取和写入(为以后聊天工具的设计基础)

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  7. 初学JDBC,调用存储过程

    在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...

  8. innerText在谷歌、火狐浏览器下的使用

    使用innerHTML.replace(/<.+?>/gim,'')代替innerText,简单正则替换一下

  9. IIS 7.5 配置10W高并发

    原文:    http://www.myhack58.com/Article/sort099/sort0100/2012/35585.htm 原文:   http://www.myhack58.com ...

  10. HttpApplication的处理管道19个事件。

    HttpApplication对象是由Asp.net帮助我们创建的,它是asp.net中处理请求的重要对象.为了便于扩展,HttpApplication采用处理管道的方式进行处理,将处理的步骤分为多个 ...