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的更多相关文章

  1. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  2. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  3. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  4. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  5. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  6. 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 ...

  7. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

  8. 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 ...

  9. 【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

随机推荐

  1. BuildTask & BuildType

    Build Tasks 在build文件中使用了Android或者Java插件之后就会自动创建一系列可以运行的任务. Gradle中有如下一下默认约定的任务: assemble 该任务包含了项目中的所 ...

  2. docker swarm join如何获取token

    在运行docker swarm join的时候需要一个token参数,如何知道这个参数那? [答案] Join as a worker node To retrieve the join comman ...

  3. NVIDIA安装显卡提示你必须先安装Intel怎么办

    无法安装驱动程序   计算机管理中查看当前的Intel的显卡驱动是否已经安装成功,如果显示的是标准VGA,则没有安装驱动,先要把Intel的显卡驱动装好(有时候360驱动大师这种工具并不能自动帮你装好 ...

  4. message sent to deallocated instance

    在XCode的以前版本中,如果遇到了 [代码]c#/cpp/oc代码: 1 message sent to deallocated instance 0x6d564f0 我们可以使用info mall ...

  5. ios Url Encode

    //ios Url Encode //有时候在请求的参数里里特殊符号比如“+”等.而如果没有encode的话那么传过去的还是” ”,面实际上是%2B. -(NSString*)UrlValueEnco ...

  6. iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)

    0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...

  7. Mysql prepare 语法

    最近一直使用语句,SELECT auction_id, auction_name,SUM(new_cart),SUM(new_collect),SUM(total_cart),SUM(total_co ...

  8. Zabbix,Nagios,OneAPM Servers 安装部署大比拼

    怎样高速实现对 Linux server的监控? 做过server监控的开发人员差点儿都知道 Zabbix 和 Nagios ,他们都是提供系统监控以及网络监控功能的开源解决方式.资历比較老.在不久前 ...

  9. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. climbing-stairs-动态规划,爬楼梯的路径数

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...