给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。

示例 1:

输入: [5,7] 输出: 4

示例 2:

输入: [0,1] 输出: 0

因为i和i + 1只相差1,所以两个数二进制的最后一位一定不一样,最后一位按位与结果一定是0。所以n,m按位与的最后一位肯定是0,我们只需要计算除去最后一位的二进制按位与的结果。即除以2后按位与的结果。

class Solution {
public:
int rangeBitwiseAnd(int m, int n)
{
if(m == n)
return m;
int cnt = 0;
while(m != n)
{
m >>= 1;
n >>= 1;
cnt++;
}
m <<= cnt;
return m;
}
};

Leetcode201. Bitwise AND of Numbers Range数字范围按位与的更多相关文章

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

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

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

    给定范围 [m,n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含m, n两端点).例如,给定范围 [5,7],您应该返回 4. 详见 ...

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

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

  4. LeetCode201 Bitwise AND of Numbers Range Java 题解

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

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

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

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

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

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

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

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

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

随机推荐

  1. linux zip,tar压缩文件夹 忽略 .git 文件夾

    linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...

  2. css悬浮在页面顶端

    .header{ position:fixed; margin-top:; width:%; z-index:; } .body{ position:relative; padding-top:119 ...

  3. QT中QString与string的转化,解决中文乱码问题

    在QT中,使用QString输出到控件进行显示时,经常会出现中文乱码,网上查了一圈,发现大部分都是针对QT4增加4条语句:</span> [cpp] view plain copy QTe ...

  4. php数据结构课程---7、队列实战

    php数据结构课程---7.队列实战 一.总结 一句话总结: 注意条件:注意循环的条件(比如while循环打印队列元素时),注意if的条件 把问题想清楚:比如链表操作初次插入元素和后面再插,效果是不一 ...

  5. shell脚本练习03--字符串

    ######################################################################### # File Name: -.sh # Author ...

  6. LeetCode 206.反转链表(Python3)

    题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...

  7. 移植 TinyLogin

    下载 从 http://tinylogin.busybox.net/downloads/tinylogin­1.4.tar.bz2下载 tinylogin­1.4 到/tmp 目录当中,并解压. 修改 ...

  8. C++ 系列:Boost Thread 编程指南

    转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...

  9. Jquery实现图片瀑布流思路-简单版

    目录 Jquery实现图片瀑布流思路-简单版 1.预备 2.开始 1.声明 2.主体 3.窗体大小改变事件 Jquery实现图片瀑布流思路-简单版 注意:本篇文章基于知道每张图片的实际尺寸的情况下 特 ...

  10. 【玲珑杯 round#18 A】计算几何你瞎暴力

    [Link]:http://www.ifrog.cc/acm/problem/1143?contest=1020&no=0 [Description] [Solution] 因为每个点的(xi ...