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. 【Git】Git-add之后-忽略部分文件的方法

    Git-add之后-忽略部分文件的方法 SparkKafkaDemo - Streaming Statistics git add 部分_百度搜索 (1 封私信)git 中如何撤销部分修改? - 知乎 ...

  2. ubuntu完全卸载apache2

    最近刚接触ubuntu和apache,第一次配置就被apahce搞到完全崩溃,跟着网上的配置修改apache的charset和apache2.conf以后,开始出现访问http://localhost ...

  3. 如何实现json字符串和 BsonDocument的互相转换

    String to BsonDocument string json = "{ 'foo' : 'bar' }"; MongoDB.Bson.BsonDocument docume ...

  4. ASP输出JSON数据及客户端jQuery处理方法

    首先ASP处理JSON需要json官方提供的JSON For ASP 封装类文件,下载地址:http://code.google.com/p/aspjson/downloads/list 下载最新的J ...

  5. Android开发——Android M(6.0) 权限解决方案

    Android开发--Android M(6.0) 权限解决方案 自从Android M(6.0)发布以来,权限管理相比以前有了很大的改变,很多程序员发现之前运行的好好的Android应用在Andro ...

  6. iOS 国际化 NSLocalizedString

    iOS 国际化.根据系统不同的语言自动切换. 首先.选择项目 Add new file -->iOS -->Resource -->Strings File  . 命名为Locali ...

  7. Github 基本操作

    .配置Git 首先在本地创建ssh key:$ ssh-keygen -t rsa -C "your_email@youremail.com" 后面的your_email@your ...

  8. JXL基本操作

    一.jxl.jar概述 通过java操作excel表格的工具类库 支持Excel 95-2000的所有版本 生成Excel 2000标准格式 支持字体.数字.日期操作 能够修饰单元格属性 支持图像和图 ...

  9. TQ2440与西门子S7-200 PLC自由口通信实现过程中问题总结

    1.在win7上安装好PLC编程软件 STEP 7 MicroWIN 之后,无法实现编程软件与PLC的通信连接? 原因:STEP 7 MicroWIN 对win7支持不是很好 解决办法:在win7中安 ...

  10. hibernate 02之helloworld

    1.安装插件 安装方法说明(hibernatetools-.Final): Help --> Install New Software... Click Add... In dialog Add ...