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. Java设计模式(六)合成模式 享元模式

    (十一)合成模式 Composite 合成模式是一组对象的组合,这些对象能够是容器对象,也能够是单对象.组对象同意包括单对象,也能够包括其它组对象,要为组合对象和单对象定义共同的行为.合成模式的意义是 ...

  2. Linq-批量删除方法

    linq中批量删除用DeleteAllOnSubmit,里面的参数是数据集 传入某要删除的ID列表,使用对象的Contains方法与数据库中值比较,相同就删除. //批量删除 public void ...

  3. Web安全学习规划

    一名合格的Web安全工程师是要具备很多的知识点,不但要对网站架构熟悉,通讯协议,测试流程与测试工具使用,漏洞利用脚本编写,还有需要经验的积累等. 互联网进入下半场,竞争越发的激烈,能与人工智能比肩的热 ...

  4. 用Telnet测试服务器的端口是否开通

      可以用telnet测试远程服务器的端口是否开通,格式如下: telnet <server name> <port number> 例如: Telnet tserv 3389 ...

  5. 详解使用DockerHub官方的mysql镜像生成容器

    详解使用DockerHub官方的mysql镜像生成容器 收藏 yope 发表于 10个月前 阅读 1506 收藏 32 点赞 1 评论 0 腾讯云·云上实验室:开发者零门槛,免费使用真机在线实验!&g ...

  6. springboot升级到2.0后context-path配置不起作用

    springboot升级到2.0后,context-path配置不起作用,改成了: server.servlet.context-path=/projname

  7. 算法笔记_221:串的简单处理(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 串的处理在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求程序对用户输入的串进行处理.具体规则如下:1. 把每个单词的首字母变为大 ...

  8. Redis问题MISCONF Redis is configured to save RDB snapshots....

    Redis问题MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on di ...

  9. 排序基础之非比较的计数排序、桶排序、基数排序(Java实现)

    转载请注明原文地址: http://www.cnblogs.com/ygj0930/p/6639353.html  比较和非比较排序 快速排序.归并排序.堆排序.冒泡排序等比较排序,每个数都必须和其他 ...

  10. 亲历H5移动端游戏微信支付接入及那些坑(三)——支付接入

    终于到接入支付了,小小的一个微信支付,居然也写了3篇,好长,好累. 接入环境 对接入环境,前端的话,应该是以js为主吧,也有可能是,PHP,Java,C++,或者C#都可以.为什么在此特意提一下接入环 ...