LeetCode201 Bitwise AND of Numbers Range Java 题解
题目:
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.
解答:
假如说5:101 7:111 连续几个数相与的规律:一,仅仅要是同样的位置的数字不同样最后那个位置的结果一定是0 。二,假设高位不同样,从不同样的那位到最低位都会为0,比如5和7尽管第0位同样可是因为第一位不同样,所以最后结果第0位 和第一位都为0。
假设理解了第二个规律就好办了,假设另个数位数不同样肯定最后结果为0。假设位数同样,从最高位開始寻找,将第一次发现不同样的那一位到最低位都置为0;
代码中,通过不断地右移直到两个数字相等。然后再左移同样的位数。这样做的效果事实上就是将位置不同样的都置为0
代码:
public static int rangeBitwiseAnd(int m, int n) {
int count=0;
while(m!=n)
{
m=m>>>1;
n=n>>>1;
count++;
}
return m<<count;
}
LeetCode201 Bitwise AND of Numbers Range Java 题解的更多相关文章
- Leetcode201. Bitwise AND of Numbers Range数字范围按位与
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 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' ...
- [leetcode] Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 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 ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
随机推荐
- partial函数-python学习
一个函数可以有多个参数,而在有的情况下有的参数先得到,有的参数需要在后面的情景中才能知道,python 给我们提供了partial函数用于携带部分参数生成一个新函数. def add(a,b,c=2) ...
- 4542: [Hnoi2016]大数
Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...
- 学习Swift -- 构造器(下)
构造器(下) 可失败的构造器 如果一个类,结构体或枚举类型的对象,在构造自身的过程中有可能失败,则为其定义一个可失败构造器,是非常有必要的.这里所指的“失败”是指,如给构造器传入无效的参数值,或缺少某 ...
- 学习Swift -- 构造器(上)
构造器(上) 构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.这个过程包含了为实例中的每个存储型属性设置初始值和为其执行必要的准备和初始化任务. 构造过程是通过定义构造器(Initi ...
- dispatch队列
GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行,它类似NSOperationQueue但更复杂也更强大,并且可以嵌套使用.所以说,结合bloc ...
- C/C++中的内存对齐 C/C++中的内存对齐
一.什么是内存对齐.为什么需要内存对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...
- java or spring +jython +python (Error:python.home,Determine if the following attributes are correct:)
最近更新 :16年3月10日更 首先你在用JAVA,你需要运行Python,于是你找了Jython,我不介绍什么是Jythyon了 如何在Java中调用Python的方法,一百度一大堆, 如下:是一种 ...
- 【UVA1371】Period (二分+DP)
题意: 给出两个字符串A,B将B分解成若干个子字符串,然后每个子字符串都要经过编辑变成字符串A,所有子串中编辑最多的次数即为当前状态下的最大编辑次数,要求求最小的最大编辑次数. 编辑操作包括修改.删除 ...
- How to Setup Chroot SFTP in Linux (Allow Only SFTP, not SSH)
1. Create a New Group Create a group called sftpusers. Only users who belong to this group will be a ...
- java.sql.SQLException: Can not issue executeUpdate() for SELECTs
未处理的多个select语句 解决方法就是:查看有没有用了同一个连接来处理多个SQL语句!