题目:

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. [android]netd与NetworkManagementService初印象

    [功能]Netd是什么,主要负责什么功能 为什么这次会接触Netd主要是因为在设置防火墙时候碰到了.关于Netd可以干什么可以从Netd的源码中CommandListener中得到答案.按照我的理解, ...

  2. NSS_02 日志配置

    采用log4net,使用系统推荐的最新版本:log4net-1.2.11-bin-newkey.zip(网址:http://logging.apache.org/log4net/download_lo ...

  3. aix 上搭建node.js 环境

    下载nodejs:ibm-4.4.3.0-node-v4.4.3-aix-ppc64.bin IBM已经适配最新版本的node.js  :https://developer.ibm.com/node/ ...

  4. laravel--belongsTo关联

    1.第一个是要引入的模型类 格式这样 belongsTo 第二个参数是拿自己这个模型表的 哪个字段 去匹配 要关联的qualified表里的哪个ID 默认是拿qualified_id去匹配,前面的是对 ...

  5. Python时间和日期学习

    #coding=utf-8 __author__ = 'Administrator' #日期和时间模块学习 """ Python程序能用很多方式处理日期和时间,转换日期格 ...

  6. 爬虫学习之基于Scrapy的网络爬虫

    ###概述 在上一篇文章<爬虫学习之一个简单的网络爬虫>中我们对爬虫的概念有了一个初步的认识,并且通过Python的一些第三方库很方便的提取了我们想要的内容,但是通常面对工作当作复杂的需求 ...

  7. [转]DRY原则和Shy原则

    转自:http://blog.csdn.net/hukeab/article/details/2944675   保障可维护性的主要诀窍是遵循DRY原则和Shy原则. 在一个系统的整个生命周期里,理解 ...

  8. 五、mysql存储引擎

    show variable like 'table_type'; 显示系统默认存储引擎 show engine\G 显示系统支持存储殷勤 =============================== ...

  9. 选中excel中的对象

    2007在查找和选择中点击“选择对象”,然后再全选全个sheet(ctrl+a)就可以看到了. 2010 “选择对象”在  开始——查找和选择——选择对象

  10. 子查询优化成join关联查询时要注意一对多关系

    mysql> select * from t where t.id in (select t1.tid from t1); +------+ | id | +------+ | +------+ ...