[Leetcode 201, Medium] Bitwise AND of Numbers Range
来源:互联网 发布:新网互联域名过户费用 编辑:程序博客网 时间:2024/06/06 03:45
Problem:
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.
Analysis:
Solutions:
C++:
int rangeBitwiseAnd(int m, int n) { if(m == n) return m; unsigned int m_temp = m; unsigned int num_digits = 0; while(m_temp != 0) { ++num_digits; m_temp /= 2; } unsigned int new_number = 1; for(int i = 0; i < num_digits; ++i) new_number *= 2; if(new_number <= n) return 0; unsigned int result = m; for(unsigned int index = m + 1; index <= n; ++index) result &= index; return int(result); }Java:
Python:
0 0
- Ubuntu 下配置Zorka 对Tomcat监控
- Yarn 内存分配管理机制及相关参数配置
- Java基础:其他对象
- 多线程编程
- 百度地图再进阶之路线规划
- [Leetcode 201, Medium] Bitwise AND of Numbers Range
- IIS请求筛选模块被配置为拒绝超过请求内容长度的请求
- jquery中ajax如何返回值到上层函数的方法以及对于js处理json对象方法的记录
- Xenserver openvswitch使用经验分享
- 论Java的跨平台性
- AFNetworking 用法
- Dialogue System for Unity文档中英对照版(简雨原创翻译)第四篇(关于主流ui插件拓展相关)
- undefined reference to vtable for
- VTK虚拟切片提取