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. Sublime Text3 插件集合

    下载地址:http://download.csdn.net/detail/yinluhui/9029791 [包含的插件有: AndyJS2.BracketHighlighter.emmet-subl ...

  2. 深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的

    前言 跟我一起顺藤摸瓜剖析 Artech 老师的 MiniMVC 是如何运行的,了解它,我们就大体了解 ASP.NET MVC 是如何运行的了.既然是“顺藤摸瓜”,那我们就按照 ASP.NET 的执行 ...

  3. Linux高级编程--07.进程间通信

    每个进程各自有不同的用户地址空间,进程之间要交换数据必须通过在内核中开辟缓冲区,从而实现数据共享. 管道 管道是一种最基本的IPC机制,由pipe函数创建: int pipe(int filedes[ ...

  4. ECMAScript5新增对象语法糖getter和setter

    在新的ECMAScript5中新添加了两个语法糖,这两个语法糖是这样的. var obj = (function(){ var num = 10; return { get n(){ return n ...

  5. LeetCode-304. Range Sum Query 2D - Immutable

    Description: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by ...

  6. HT全矢量化的图形组件设计

    HT一直被客户称道的就是其全矢量化的设计特色,矢量相比传统图片好处太多了: 矢量可无级缩放,界面不失真不模糊 描述矢量的文本内容远比图片小得多 目前各种window.devicePixelRatio不 ...

  7. BZOJ1087状压DP 解题报告

    1087: [SCOI2005]互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的 ...

  8. 本学期3个sprint的团队贡献分

    第一次冲刺贡献分 组员 贡献分 103马嘉诚 28 143李新佳 22 145马文其 19 120韩智豪 16 147黄鸿浩 15 第二次冲刺贡献分 组员 贡献分 103马嘉诚 23 143李新佳 2 ...

  9. Linq查询操作之Where筛选

    筛选操作where能够处理逻辑运算符组成的逻辑表达式.比如逻辑“与”,逻辑“或”,并从数据源中筛选数据,它和where子句的功能非常相似.Enumerable类的Where()原型如下: public ...

  10. java之内的工具分享,附带下载链接,方便以后自己寻找

    class反编译工具:http://pan.baidu.com/s/1geYvX5L redis客户端工具:http://pan.baidu.com/s/1eRJ4ThC mysql客户端-[mysq ...