Bitwise AND of Numbers Range
来源:互联网 发布:php 项目怎么维护的 编辑:程序博客网 时间:2024/06/07 00:32
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,那么最末位必定等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。
class Solution {public: int rangeBitwiseAnd(int m, int n) { int offset = 0; while(m != n) { m>>=1; n>>=1; offset++; } return m<<offset; }};
http://blog.csdn.net/yusiguyuan/article/details/45165589
http://www.cnblogs.com/aboutblank/p/4442193.html
0 0
- 关于PopuWindow的一个显示与隐藏切换的问题
- MFC中静态文本框贴图处理
- 获取后台数据中文有乱码的解决办法
- 【从MVC框架看MVC架构的设计】
- python os模块
- Bitwise AND of Numbers Range
- Linux下查看80端口是否被占用
- HI3516A模组与开发板产品出来了,效果非常OK!
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案
- iOS 利用mask layer 使view中扣掉一块露出下边的view
- js 去掉字符串前后空格
- iOS 从一个app打开另一个app
- iOS开发——为iOS工程创建模拟器包,用于其它电脑上iOS模拟器的运行测试(Creating an iOS Simulator Build)
- BLE开发