【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range
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.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
从m到n逐个做与操作肯定是不合理的,最多要进行INT_MAX*32次位与操作。
可以把复杂度降低到32次移位并处理。
对于每一位来说,只要中间存在一个0,该位就是0,只有全1的时候才是1.
因此问题变为:对于从右数第i位数字,从m到n之间是否全1?
满足全1要同时满足两个条件:
(1)m的相应位置是1 即起始位置必须是1
(2)m到n之间的间隔不大于m到最后一个连续1的间隔
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int ret = ;
int gap = n - m;
if(gap == )
return m;
int bit = ;
//note that 0 <= m <= n <= 2147483647
//the highest bit must be 0, thus skip i == 31
for(int i = ; i < ; i ++)
{//bit by bit check zero
int ind = m % (int)pow(2.0, i+);
if((ind >= (int)pow(2.0, i)) && (ind+gap <= (int)pow(2.0, i+)-))
//all 1's
ret |= bit;
bit <<= ;
}
return ret;
}
};

【LeetCode】201. Bitwise AND of Numbers Range的更多相关文章
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- Java for LeetCode 201 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
Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...
- leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
随机推荐
- GLFW_KEY_KP_ADD和GLFW_KEY_KP_SUBTRACT
这两个键的代码分别为: GLFW_KEY_KP_ADD(334) GLFW_KEY_KP_SUBTRACT(333) 对应的是键盘右侧数字面板上的+ -键.
- jdbc:initialize-database标签的研究
在spring的applicationContext.xml中如果引入了:<?xml version="1.0" encoding="UTF-8"?> ...
- iOS开发技巧 - 使用UIPickerView来选择值
(Swift) import UIKit class ViewController: UIViewController, UIPickerViewDataSource { var picker: UI ...
- iOS编程(双语版) - 视图 - 手工代码(不使用向导)创建视图
如何创建一个空的项目,最早的时候XCode的项目想到中,还有Empty Application template这个选项,后来Apple把它 给去掉了. 我们创建一个单视图项目. 1) 删除main. ...
- 很好的vmware目录
http://www.globalknowledge.com/training/course.asp?pageid=9&courseid=18023&country=United+St ...
- 递归的隐含限制——处理对象小的可以、大的不可以
最近自己编写了一个求n阶行列式的值的C程序,编译成功,并且使用了一个3阶行列式进行了测试,测试也成功了.以为这样就万事大吉了,可是后来在实际应用中调用该函数时却导致程序无法运行.注意到,实际应用中要求 ...
- POSTGRESQL 锁表的问题
一.找出所的语句 select wait.pid, wait.query as wait_query, wait.query_start as wait_query_start, wait.lockt ...
- Aerospike系列:5:安装AMC
1:需要安装的包,如果缺少,请安装. python (2.6+) gcc python-devel 安装相应的模块 sudo pip install markupsafe sudo pip insta ...
- Struts2学习笔记二:开发流程
一:创建项目,添加依赖包 二:在web.xml配置核心控制器 <filter> <filter-name>struts2</filter-name> <fil ...
- Java JDBC编程套路教程
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5847020.html 学习Java开发,一个必须掌握的知识点,就是数据库操作.当程序需要用到的数据达到一定程 ...