LeetCode 201. Bitwise AND of Numbers Range
来源:互联网 发布:舞霸王跳舞软件 编辑:程序博客网 时间:2024/06/03 17:02
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.
Suppose our given range is 5 : 0101 ---- 7: 0111
Thus, we can see that the lower 2 bits are different. By shifting them to right, we can finally make m == n. thus, result will be m << count;
Code copied from the discussion. But this method is very fantastic!!
int rangeBitwiseAnd(int m, int n) { int count = 0; while(m != n) //until the left identical bits, all the right will be cancelled by increments from m to n;; { m >>= 1; n >>= 1; count++; } return m << count; }
0 0
- st032
- Codewars-Javascript训练手册:正则表达式(上)
- Android学习 -- studio的快捷键们
- MyEclipse的server报错:An unexpected exception was thrown”
- Mac ssh远程登录Linux
- LeetCode 201. Bitwise AND of Numbers Range
- 尝试运行项目时出错,无法启动程序,拒绝访问 或其他类似提示解决办法
- 【CodeForces】Registration system
- R语言-图像处理
- Unity的数据序列化与反序列化用于网络传输
- LeetCode 252. Meeting Rooms
- ERROR 1045 (28000): Access denied for user root@localhost (using password: YES)
- Android地面站-MavLink解析部分源码
- LeetCode 171. Excel Sheet Column Number