题目:

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. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  2. 何谓IOC的核心思想

    IOC(Inversion of Control)即控制反转,是在面试或平常交流中经常遇到了词汇:我也曾经仿照Spring,利用JDK的反射和动态代理实现了一个简单的IOC框架,感觉算是知其然也知其所 ...

  3. Entity Framework Code First 迁移数据库

    新版EF,系统实现过程中如果对Model进行更改,队形修改数据库并不能正常运行项目,需要借助Code First 手动迁移数据库 首先启用迁移,允许迁移Context Tools->Librar ...

  4. centos7搭建NIS与NFS综合应用

    实验环境: centos7(服务端)        redhat enterprise linux 7.2(客户端) 实验目的:用centos7的账号,能在redhat enterprise linu ...

  5. StringBuilder的Append()方法会比+=效率高

    StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id from " + databa ...

  6. tomcat源码解读(2)–容器责任链模式的实现

    责任链模式:责任链模式可以用在这样的场景,当一个request过来的时候,需要对这个request做一系列的加工,使用责任链模式可以使每个加工组件化,减少耦合.也可以使用在当一个request过来的时 ...

  7. soinn

    Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...

  8. AngularJS快速开始

    Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...

  9. 怎样下载安装Firebug和使用Firebug

    Firebug是基于火狐(FireFox)浏览器的一个插件,它的作用是给Web页面开发者一个很好的测试前端页面代码的工具.所以深受网页开发者或网页布局爱好者的喜爱.像我们用DIV+CSS和html所写 ...

  10. 简单的异步任务工具——rq 的使用教程

    rq是一个简单的,轻量级的异步任务工具. 如果在网站中用户发起一个用时很久(大于2分钟)的请求,如果用同步的方式,服务器就会返回超时. 这时候就需要用异步请求,用户发起请求后,服务端把作业扔给另一个进 ...