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.

Have you met this question in a real interview?
 
思路:
找规律即可,通过分析我们发现:
1,当 m = n时,直接返回m或者n即可。
2,当表示m和n的二进制的数的长度不相等时,由于低一位像高一位进位时,出现的数必然为100...0,长度比低位的数长了一位,此数与长度比它小的数“与”,结果必然为0,0“与”任何数都为0。即,当表示m,n的二进制数的长度不一致时,从m到n进行“与”操作,结果为0;
3,当二者长度一致时,进行循环与操作即可。只是要注意:n为INT_MAX 时,需要特殊处理。
 
class Solution
{
public:
int rangeBitwiseAnd(int m, int n)
{
int ret = m;
int a = m, b = n;
int lena = , lenb = ; if(m == n)
return m; while(a >>= )
lena++; while(b >>= )
lenb++; if(lena == lenb)
{
for(int i = m + ; i <= n; i++)
{
ret &= i;
if(i == INT_MAX)
break;
}
}
else
ret = ; return ret;
}
};

[leetcode] Bitwise AND of Numbers Range的更多相关文章

  1. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  2. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

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

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

  4. 【LeetCode】201. Bitwise AND of Numbers Range

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

  5. LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range

    1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...

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

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

  7. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

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

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

  9. 【leetcode】Bitwise AND of Numbers Range(middle)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

随机推荐

  1. c++ 相关的技术资源整理归类

    最近一段时间 c++ 社区里最火热的话题莫过于 cppcon2015 了, isocpp 上一堆相关的新闻,其中有一个页面罗列了该会议的全部主题, 匆匆一瞥几乎眼花缭乱,为期一个星期的会议竟有上百个演 ...

  2. PIN码计算锦集

    1. 腾达,C8:3A:35开头的MAC有效~network路由,MAC有效~以及00B00C开头的MAC有效之外的请您自己发现算法..这里只公布三个MAC地址算法,其余也可以算~这里就不公布出来了. ...

  3. Tips10:你可以像写文档一样自由的复制粘贴游戏组件(Game Object Component)

    相对于添加组件后再进行调整和赋值,通过复制和粘贴已有游戏组件能够带来更高的效率.

  4. Ladda – 把加载提示效果集成到按钮中,提升用户体验

    Ladda 是一组集成了加载提示的按钮,以弥合行动和反馈之间的时间间隔,提供更好的功能使用体验.主要用于在用户点击提交之后,向用户提供即时的反馈,让他们知道浏览器正在处用户提交的任务. 您可能感兴趣的 ...

  5. .NET中Main函数使用小技巧

    摘要:任何语言开发出来的程序,都会有一个程序入口函数,可能每个语言所使用的程序入口函数名称不一样,但是它们的作用都是一样的,都是被操作系统去调用.那么本文主要总结.NET中的程序入口函数Main使用的 ...

  6. 0422 发现( 数学口袋精灵)bug

    团队博客地址: 甘佳萍:http://www.cnblogs.com/gjpg/ 李鹏飞:http://www.cnblogs.com/l549023320/ 赵创佳:http://www.cnblo ...

  7. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  8. Ext.NET 4.1.0 搭建页面布局

    Ext.NET目前的最新版本为4.1.0,可以从官网:ext.net上下载,具体下载网址为:http://ext.net/download/. 文件下载下来后,在\lib\目录下存在3个文件夹,分别对 ...

  9. c#调用word com组件 替换书签套打

    安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...

  10. 2. npm 的使用

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...