Bitwise AND of Numbers Range——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.
题目大意:给一个范围,返回这个范围中所有的数按位相与最后的结果。
解题思路:当拿到这个题目的时候,我是拒绝的,这么简单的题,直接与然后返回不就是了,然后发现范围太大会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的更多相关文章
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- [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
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 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 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
随机推荐
- MYSQL 体系结构图
- mysql @变量和变量的区别及怎么判断记录唯一性
DELIMITER// drop PROCEDURE if EXISTS test.express; create PROCEDURE test.express() BEGIN ) into @a f ...
- iOS多线程编程之GCD的使用
什么是线程呢? 1个CPU执行的CPU命令列为一条无分叉的路径即为线程. 这种无分叉路径不止1条,存在多条时即为多线程. 什么是GCD? Grand Central Dispatch (GCD)是异步 ...
- Proxy 代理模式
简介 代理模式是用一个简单的对象来代替一个复杂的或者创建耗时的对象. java.lang.reflect.Proxy RMI 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对 ...
- codevs 1746 贪吃的九头龙
/* 状态定义的没错 就是考试的时候傻啦吧唧的转移左右孩子 其实之转移父亲就简单多了 不用考虑那么多 还有就是偷懒没有把谁有没有找过这个信息转过去 而是搞了个全局变量…wa到挺 再就是特盘的时候还有终 ...
- (转)ThinkPHP3.0 使用分组后路径访问无效的解决方法!
注意,清除Runtime,就是清除缓存,很重要,妹的,调试了一下午,总是加上Home目录分组就找不到页面,直接放到action下就行,原来是缓存搞得鬼,另外要在入口文件开启‘APP_DEBUG’ 在T ...
- UISegmentControl 、UIStepper
UISegmentControl .UIStepper UISegmentControl 1. UISegmentedControl *segmentControl = [[UISegmentedCo ...
- 读取url(1
就书本例子 import java.io.InputStream; import java.net.URL; public class Test { public static void main(S ...
- 【USACO 3.3.1】骑马修栅栏
[描述] Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个程序,读入 ...
- jquery 文本框聚焦文字删除
做作业需要,自己写了一个,写的很烂. $(function() { $("#search_input").addClass("before_focus");/* ...