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. 站内信,群发与全部发送。Gson解析result

    /** * 发送站内信 */@Permission(Module.TZGL)@RequestMapping(value = "/sendznx", method = Request ...

  2. Android(java)学习笔记222:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  3. Android中px、dp、sp的区别

    px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guid ...

  4. Linq101-Restriction

    using System; using System.Linq; namespace Linq101 { class Restriction { /// <summary> /// Thi ...

  5. iOS在Cocoa Touch Static Library使用CocoaPods

    1.在XCode中新建静态库工程:DDLogLib. 2.添加对外暴露接口的头文件DDLogLibHeader.h 3.命令行进入DDLogLib目录,运行pod init,并修改Podfile 4. ...

  6. 17个提升iOS开发效率的必用工具

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我们提升编码速度和工作效率的工 ...

  7. 64位操作系统下IIS报“试图加载格式不正确的程序”错误

    缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环境下的,要么全是32位环境下的.不能混编访问.不然 ...

  8. 2.2.1 创建一个 Path

    Demo: import java.nio.file.Path; import java.nio.file.Paths; /** * @author jinxing * @系统 MAC OS X * ...

  9. 深入了解使用egret.WebSocket

    概念 本教程不讲解TCP/IP协议,Socket属于哪层,消息包体怎么设计等,主讲 egret.WebSocket 使用示例 与 protobuf 使用示例. 在使用egret.WebSocket之前 ...

  10. 实用iPhone Cydia插件

      BadgeClear(角标清除):可以清除App推送所在图标右上角出现的红色角标.在桌面长按图标后,图标开始左右摇动,再双击图标即可清除点击的图标角标.   Bitesms(短信):收费插件,具有 ...