题目:

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. UVALive 3645 Objective: Berlin(最大流 :时序模型)

    题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...

  2. 模板:多Case输入处理

    利用cin实现 while(cin >> value) { } 调试时使用Ctrl + Z 输入文件结束符

  3. nutch安装配置

    http://nlp.solutions.asia/?p=180 http://www.promenade.me/archives/146 环境 ubuntu 12.04 sql建表 CREATE D ...

  4. Web前端的35个jQuery小技巧

    1. 禁止右键点击 $(document).ready(function(){     $(document).bind("contextmenu",function(e){   ...

  5. phpcms v9修改栏目描述的多行文本为编辑器方法

    phpcms v9在添加栏目的时候,栏目描述为多行文本,无法满足有图片,以及格式的修改调整,那么仿站网今天告诉大家如何将他改为编辑器,方法如下 找到phpcms/moudles/admin/templ ...

  6. cxgrid HyperLink 鼠标显示效果

    procedure TForm1.cxGrid1DBTableView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); v ...

  7. 从浏览器启动应用程序 - Application URL

    关键字:Browser,Application,URL Protocol,Windows,Mac,IE,Chrome,Safari. OS: Windows 7, OS X Yosemite. Win ...

  8. cwm-recovery自动生成工具

    android发展迅速,刷机已经成了一种习惯,cwm-recovery是必不可少的工具,下面我把自己用的自动生成cwm-recovery的工具发布出来,供友友们交流和学习,欢迎拍砖 已经公开发布在gi ...

  9. sqlplus中"-S"和"-L"用法

    Usage: SQLPLUS [option] [logon] [start] <option> ::= -H | -V | [ [-L] [-M ] [-R ] [-S] ]   &qu ...

  10. 另一个 SqlParameterCollection 中已包含 SqlParameter

    出处:http://www.cnblogs.com/OldYongs/archive/2011/03/12/1982021.html#2742742 一般情况下,我们定义的一个SqlParameter ...