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. 《Android开发艺术探索》读书笔记 (10) 第10章 Android的消息机制

    第10章 Android的消息机制 10.1 Android消息机制概述 (1)Android的消息机制主要是指Handler的运行机制,其底层需要MessageQueue和Looper的支撑.Mes ...

  2. Android实现网络多线程断点续传下载(转)

    本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 我们编写的是Andorid的HTTP协议多线程断点下载应用程序.直接使用单线程下载HTTP文件对我们来说是一件非常简单的事.那么,多 ...

  3. Day9 - Python 多线程、进程

    Python之路,Day9, 进程.线程.协程篇   本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...

  4. dedecms调用所有顶级栏目最新文章的实现方法

    做dedecms的模板,我们会遇到各种各样的调用问题,dedecms列表页调用所有顶级栏目文章列表的方法如下所述: 在文章页面经常使用的是 {dede:arclist orderby='pubdate ...

  5. 昨天做了一个使用javamail发送文件的demo

    记录一下过程. 两种版本第一个demo是纯java文件.一个就可以,是我在网上搜索到的,第二个demo是我在ssh框中中写的jsp页面demo 1.java版本: package com.zq.www ...

  6. sqlserver 将多行数据查询合并为一条数据

    有这样一个需求:表T_FUN_TASK为任务表,有字段(TASKID,TASKNAME),表T_FUN_LOGBOOK为日志表,有字段(LOGID,TASKID,LOGDATE),一个任务可持续多天, ...

  7. 用Hopper搞定Mac迅雷的会员以及离线下载功能

    转自 用Hopper搞定Mac迅雷的会员以及离线下载功能 先定位Mac迅雷的可执行文件 snakeninnys-iMac:~ snakeninny$ ls /Applications/Thunder. ...

  8. 深入理解offsetTop与offsetLeft

    做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框.那么问题来了,包含元素off ...

  9. javascript 原生 cookie 处理

    来自网络! function getCookie(name) { var start = document.cookie.indexOf(name + "="); var len ...

  10. jQuery解析JSON的问题

    在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. JS ...