题目:

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://leetcode.com/problemset/algorithms/

题解:

一开始采用暴力解,自然超时了。后来想了想,其实每次比较一个位置就可以了变成0以后不可能再变回1,所以能不能转换为一个O(32)的运算。因为从m到n的话其实就等于m + 1 + 1 + 1...  ~ n,假设从101000变换到110000,低5位总会被全部清0, 这样我们只用从最高位往最低位置比较m和n的相同部分就可以了,或者从最低位向最高位比较,假如m和n个位不相等,则向右shift,继续比较十位,以此类推。然而想到这里并没有什么用,还没写code我就去看了discuss...于是得到了答案....要改掉这个坏毛病。

Time Complexity - O(1), Space Complexity - O(1)

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

还有另外一种更简练的写法在reference里。就是对n进行末位清0,然后于m进行比较,直到n <= m,然后返回n。

Time Complexity - O(1),Space Complexity - O(1)。

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
while(m < n)
n = n & (n - 1); return n;
}
}

二刷:

用了上面的办法。就是把n从最低起清零,然后跟m进行比较,当m >= n的时候,这时候我们找到了一个可能的解,返回n。

我们也可以从高位向低位比较。

Java:

Time Complexity - O(1), Space Complexity - O(1)

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
while (m < n) n &= (n - 1);
return n;
}
}

三刷:

利用末位清零。

Java:

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
while (m < n) {
n &= (n - 1);
}
return n;
}
}

Reference:

http://www.meetqun.com/thread-8769-1-1.html

https://leetcode.com/discuss/32115/bit-operation-solution-java

https://leetcode.com/discuss/32278/8line-c-simple-clear-solution

https://leetcode.com/discuss/32053/accepted-c-solution-with-simple-explanation

https://leetcode.com/discuss/35057/share-my-simple-java-solution

https://leetcode.com/discuss/34918/one-line-c-solution

https://leetcode.com/discuss/53646/simple-and-easy-to-understand-java-solution

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

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

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

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

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

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

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

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

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

  5. Java for LeetCode 201 Bitwise AND of Numbers Range

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

  6. 201. Bitwise AND of Numbers Range -- 连续整数按位与的和

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

  7. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

  8. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  9. 201. Bitwise AND of Numbers Range (Bit)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND(按位与) of all nu ...

随机推荐

  1. Windows7下安装搭建play框架

    作者:Sungeek 出处:http://www.cnblogs.com/Sungeek/ 欢迎转载,也请保留这段声明.谢谢! 1.首先官网下载play的解压包 https://playframewo ...

  2. Java编程思想之字符串

    来自:Java编程思想(第四版) 第十三章 字符串   字符串操作是计算机程序中最常见的行为.   String对象是不可变的.查看JDK文档你就会发现,String类中每一个看起来会修改String ...

  3. 第三章 用SDK编译出第一个在Linux下的软件界面

    第三章 用SDK编译出第一个在Linux下的软件界面 先创建一个工程目录“mkdir project1”,进入目录,创建main.cpp文件,编写代码如下: 代码内容暂时可以先不理解,先让程序跑起来再 ...

  4. CentOS 下安装JDK

    前提条件 使用干净的centOS 之前肯定没有装过JDK 所以忽略卸载步骤 <1>从SUN下载jdk-1_5_0_14-linux-i586-rpm.bin或jdk-1_5_0_14-li ...

  5. Qt移植 Window --Linux

    1.把源代码复制到Linux目录,使用qmake命令,注意在shell中直接使用qmake命令注意设置PATH环境变量 2. 在目录中会生成Makeflie文件 3. make即可 /usr/bin/ ...

  6. Jquery 页面元素动态添加后绑定事件丢失方法,非 live

    代码1: 以此方法绑定的input框事件,在通过add按钮后用jquery绑定的事件 alert就会丢失 <input type="button" value="A ...

  7. 修改zepto源代码,使支持wp8的ie10

    注意:当前1.1.3版本的zepto,已经有模块来支持wp8 原先的zepto,通过__proto__赋值,来使dom继承到$.fn方法, 无奈IE11之前的IE10,IE9不支持这种写法, 所以我们 ...

  8. 页面有什么隐藏bug:字体,图片

    字体: 一行(太长)-display:inline-block,text-overflow: ellipsis;max-width:xxpx 多行(太高,太矮)-设置max-height,min-he ...

  9. Review PHP设计模式之——单例模式

    单例模式: class Single { private static $_instance; private function __construct(){ //define method as p ...

  10. django1.6之template基础用法

    >>> settings.configure()>>> tem=template.Template("my template is {{name}}&qu ...