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.

题目大意:给一个范围,返回这个范围中所有的数按位相与最后的结果。

解题思路:当拿到这个题目的时候,我是拒绝的,这么简单的题,直接与然后返回不就是了,然后发现范围太大会TLE,后来想范围里有一个数某位是0,其他的就不用判断了,肯定是0,然后写出来这个解法,

public int rangeBitwiseAnd(int m, int n) {
int res = 0;
int[] bits = new int[32];
Set<Integer> offset = new HashSet<>();
for (int i = 0; i < 32; i++) {
bits[i] = 1;
}
out:
for (int j = 0; j < 32; j++) {
for (int i = m; i <= n; i++) {
if (bits[j] == 0)
break;
if ((i & (1 << j)) == 0) {
bits[j] = 0;
offset.add(j);
}
if (offset.size() == 32 || i == Integer.MAX_VALUE) {
break out;
}
}
}
for (int i = 0; i < 32; i++) {
res += bits[i] << i;
}
return res;
}

结果还是TLE。

然后看了别人的解法,更巧妙,当m!=n,那么最末位必定等0,因为[m,n]必定包含奇偶数,相与最末位等0。当m=n的时候,后面都是0,前面的就是这个范围内的数按位相与的相同部分。

举例来说:m=4(0000 0100), n=6(0000 0110), 那么范围[4,6]中包含4、5、6,即0000 0100, 0000 0101, 0000 0110,所有的结果按位与得到0000 0100。

初始:m!=n,于是m,n分别右移一位得到0000 0010, 0000 0011,同时偏移量offset+1;

m!=n,于是m,n继续右移一位得到0000 0001, 0000 0001,同时偏移量offset+1;

m=n,得到结果m<<offset。

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

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

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

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

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

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

  3. [leetcode] Bitwise AND of Numbers Range

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

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

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

  5. 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 ...

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

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

  7. 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' ...

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

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

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

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

随机推荐

  1. The builder launch configuration could not be found

    Export Wizard Error      Errors occurred during the build Problems occured when invoking code from p ...

  2. PuTTY?Bash?Out了!!!终端应该这么玩~

    由于语言的障碍,国内一直存在一个问题,就是新技术引入太慢.比如PuTTY,其实已停止维护N久了,但大部分人却仍然在用(包括之前的我).比如Bash,明知有那么多的问题,却一直没有什么想法,似乎Linu ...

  3. Python分类统计数据

    在数据的常见分布中,有一种是一对多存储的数据,即一个是key,其他改key对应的多个value.例如气象数据等,每天有很多组,又或者是一个球员,他每天得多少分等等.我做这个东西有三种方法,即:常规编程 ...

  4. Linux开发工具之gdb(上)

    三.gdb调试(上) 01.gdb:gdb是GNU debugger的缩写,是编程调试工作. 功能:   启动程序,可以按照用户自定义的要求随心所欲的运行程序:   可让被调试的程序在用户所指定的调试 ...

  5. Android音频开发之——如何播放一帧音频

    本文重点关注如何在Android平台上播放一帧音频数据.阅读本文之前,建议先读一下<Android音频开发(1):基础知识>,因为音频开发过程中,经常要涉及到这些基础知识,掌握了这些重要的 ...

  6. node.js的ejs模版引擎

    ejs版本是0.8.8,生成的views目录下面只有index.ejs and error.ejs,没有layout.ejs. D:\lianchuangfile\nodeDevelop\microb ...

  7. ASP.NET 日志

    ASP.NET5已经内建集成了日志系统,你也可以用第三方的日志框架. 1. 在你的应用程序中实现日志 在Startup.cs文件中,在Configure里可以把ILoggerFactory注入进去,然 ...

  8. 学习springMVC实例1——配置和跳转到HelloWorld

    本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架 一.用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090 二.在WEB-INF目录下 ...

  9. uvalive 5760 Alice and Bob (组合游戏,dp)

    题目链接: http://vjudge.net/problem/viewProblem.action?id=25636 对于>1的堆,必然会被其中一人全部合并. 然后就是二维dp,dp[非1堆的 ...

  10. POJ 1155 树形背包(DP) TELE

    题目链接:  POJ 1155 TELE 分析:  用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理.        dp[cnt][i+j] = max( dp[cnt][i+j ...