Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目

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.

题意:

给出m和n,将m到n之间所有的数字与一遍返回结果(包括m和n)

解题思路:

题目乍一看很简单嘛,循环遍历m到n按位与一遍返回结果就完了啊,但是将这种解法放入LeetCode会发现超时,那么就需要我们去总结规律。

例如题目给出的5,6,7二进制

0101   0110   0111

通过观察我们会发现我们只要看数字的高位即可,后面的数字会相互约掉

再比如

011000  0110001  0110010 0110011

我们发现貌似只要查看所有数字的高k位即可

再比如

01100  01101 01110  01111  10000

我们比较发现如果m和n的位数不相同就会返回0

规律如下:

给出m和n,求出m和n相同的高k位(这里m和n为整型变量,那么按照32位来看待,前面的0也算作高位比较),那么保留高k为其余位置0即为最后结果。

代码如下:

一开始我选择从低位遍历,记录需要舍弃的低l位

代码1

     public int rangeBitwiseAnd(int m, int n) {
if (n == m)
return m;
int h = 0;
int l = 0;
int mm = m;
while (m > 0 || n > 0) {
if ((m & 1) == (n & 1)) {
m >>= 1;
n >>= 1;
++h;
}
else {
m >>= 1;
n >>= 1;
l += h;
++l;
h = 0;
}
}
if(h==0)return 0;
return mm & (0x7fffffff << l);
}

通过后查看代码其实直接从高位开始遍历到第一个不相同的位停下即可找到高k位,代码如下

代码2

     public int rangeBitwiseAnd2(int m, int n) {
if(m==n) return m;
int i;
for(i = 31;i>0;i--){
if((m&(1<<i))!=(n&(1<<i)))
break;
}
return m&(~((1<<i+1)-1));
}

代码简洁了很多,也比原来的快了很多

题目再次利用了Java中的移位操作,记录下做题中遇到的二进制的几个问题:

1.整数x求-x,从二进制角度看是将x按位取反加1

2.左移正负数都是低位补0,右移正数高位补0负数补1,而如果使用Java无符号右移>>>则正负高位都补0

3.将整数按照二进制整个翻转可以利用类似归并排序的分治思想

  (1) 整数32为分成16和16先翻转一下

  (2) 再翻转前16位中的前8位和后8位,再翻转后16位中的前8位和后8位

  。。。以此类推,总共需要Log232 = 5次

代码如下:

     public int reverseBit(int n) {
n = ((n & 0xffff0000) >>> 16) | ((n & 0x0000ffff) << 16);
n = ((n & 0xff00ff00) >>> 8) | ((n & 0x00ff00ff) << 8);
n = ((n & 0xf0f0f0f0) >>> 4) | ((n & 0x0f0f0f0f) << 4);
n = ((n & 0xcccccccc) >>> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xaaaaaaaa) >>> 1) | ((n & 0x55555555) << 1);
return n;
}

注意这里是将整个32位全部翻转,相当于32位倒过来看。

举例,按照4位来看

1110

(1)将前2位和末2位翻转 10  11

(2)将第一部分对调,第二部分对调得到结果  01 11

Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range的更多相关文章

  1. leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

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

  2. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0

    https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...

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

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

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

  5. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

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

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

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

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

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

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

  9. Java位运算总结-leetcode题目

    按位操作符只能用于整数基本数据类型中的单个bit中,操作符对应表格: Operator Description & 按位与(12345&1=1,可用于判断整数的奇偶性) | 按位或 ^ ...

随机推荐

  1. Http状态码之:301、302重定向

    概念 301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有链接编辑功能的客户端应当自动把请求的地 ...

  2. Windows 7上执行Cake 报错原因是Powershell 版本问题

    在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...

  3. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  4. OpenCASCADE Shape Location

    OpenCASCADE Shape Location eryar@163.com Abstract. The TopLoc package of OpenCASCADE gives resources ...

  5. 利用CSS中的:after、: before制作的边三角提示框

    小颖昨天分享了一篇参考bootstrap中的popover.js的css画消息弹框今天给大家再分享一篇使用:before和:after伪元素画消息弹框的CSS. 画出来是介个酱紫的: 有没有觉得画的萌 ...

  6. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-补充WebApi与Unity注入-配置文件

    系列目录 上一篇演示了WebApi利用Unity注入 很多人问我如何用配置文件来配置注入,本节演示如何利用配置文件来注入,道理是一样的,跳转到上一节下载源码一起来动手! 1.打开源码定位到文件Depe ...

  7. JAVA构造时成员初始化的陷阱

    让我们先来看两个类:Base和Derived类.注意其中的whenAmISet成员变量,和方法preProcess(). 情景1:(子类无构造方法) class Base { Base() { pre ...

  8. Log4Net应用问题

    问题 一.日志存储方式 1.txt 2.SQLServer数据库 3.log文件 二.项目类型不同 1winFrom 2webFrom 3MVC 4WPF 5控制台 三.切分依据不同 1.空间大小 2 ...

  9. Mysql - 游标/动态sql/事务

    游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...

  10. Tomcat之APR错误

    在发布Apache Tomcat的时候,突然出现如下错误: An incompatible version 1.1.31 of the APR based Apache Tomcat Native l ...