[LeetCode 201] Bitwise AND of Numbers Range
2015-09-13 13:49
417 查看
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.
solution:
bit manipulation, calculate common 1 prefix between m and n;
For example, given the range [5, 7], you should return 4.
solution:
bit manipulation, calculate common 1 prefix between m and n;
public int rangeBitwiseAnd(int m, int n) { if(m == n) return m; int bit = 0; while(m!=n) { m=m>>1; n=n>>1; bit++; } return m<<bit; }
相关文章推荐
- 安装oracle11g学习版遇到的几个问题
- 数据科学家应该具备的知识
- autoLayout-自动布局
- 1102. Invert a Binary Tree (25)
- java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException 异常解决方案
- Task '' not found in root project '***'.
- Python+OpenCV学习(10)---SURF
- Linux vim(4)
- LintCode "Minimum Subarray"
- QT5.5+vs2010环境搭建
- WIN8/WIN10下安装老式打印机驱动
- android学习笔记--单元测试
- github 下载 单个 部分 文件夹
- 线性表的应用,计算任意两个表简单自然连接过程讨论线性表的应用(参考数据结构教程 李春葆)
- SIZEOF
- 第二篇 Mysql常用操作记录(转载)
- 目录权限
- SQLite的使用
- 1101. Quick Sort (25)
- 分段选择控件UISegmentedControl、UISwitch、UISlider