LeetCode201 Bitwise AND of Numbers Range Java 题解
JAVA and of 题解 range Numbers
2023-09-14 09:07:56 时间
题目:
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;
}
相关文章
- myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).
- 深入Java虚拟机读书笔记[8:9]
- java中复制文本文件
- Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)
- Java实现 LeetCode 419 甲板上的战舰
- Java实现哥德巴赫猜想
- java实现第六届蓝桥杯星系炸弹
- Java实现 蓝桥杯VIP 算法提高 计算时间
- Java实现 蓝桥杯VIP 算法提高 3000米排名预测
- (Java实现) 装载问题
- [Java Spring data] Paging and sorting
- java修改文件所有者及其权限
- [Java Spring] Built-in page and sorting
- null in ABAP and nullpointer in Java
- Atitit. Async await 优缺点 异步编程的原理and实现 java c# php
- atitit.人脸识别的应用场景and使用最佳实践 java .net php
- Atitit.ati orm的设计and架构总结 适用于java c# php版
- Atitit.ati orm的设计and架构总结 适用于java c# php版
- atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践
- On Memory Leaks in Java and in Android.
- Android10以上源码中编译java及UI(一百一十九)
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- @Value的使用 《Spring源码解析》java笔记