题目

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.

分析

题目字面意思是给定两个整数构成闭区间, 0 <= m <= n <= 2147483647 均为合法整数,求区间内所有整数的位与结果。

看起来是一个很简单的题目,我们可以直接一次遍历得到结果,复杂度为O(n),意料之中的超时。。。

那么,换一种方法,可不可以采用递归呢? 两段分别求出结果,然后相与得到最终结果,遗憾的是再次TLE。。。

看来,不能简单的想当然解题,不妨写出每个整数的二进制表示分析一下:

5 0101

6 0110

7 0111

&

0100

有什么规律呢,我们可以看出结果中的“1”是所有数字的所共有的位;这样就有高效的解法了,我们可以利用移位的规则,将数字相异的位右移掉,记录需要移位的个数。

详细代码见下节。

AC代码

class Solution {
public:
//方法一,一次遍历 TLE
int rangeBitwiseAnd1(int m, int n) { int ret = m;
for (int i = m+1; i <= n; ++i)
{
ret = ret & i;
}//for
return ret;
} //方法二,采用递归,TLE again!
int rangeBitwiseAnd2(int m, int n) {
if (m == n)
return m; int mid = (m + n) / 2;
return rangeBitwiseAnd(m, mid) & rangeBitwiseAnd(mid + 1, n);
} //方法三:
int rangeBitwiseAnd(int m, int n) {
int offset = 0;
while (m && n)
{
//找到最高相同位
if (m == n)
{
return m << offset;
}
m >>= 1;
n >>= 1;
offset++;
}
return 0;
}
};

GitHub测试程序源码

LeetCode(201) Bitwise AND of Numbers Range的更多相关文章

  1. 【LeetCode 201】Bitwise AND of Numbers Range

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

  2. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  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)

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

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

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

  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. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. Shell分割字符得到数组

    #!/bin/bash p=$(hadoop fs -ls /tgl/data |awk '{print $8}') #要将$a分割开,先存储旧的分隔符 OLD_IFS="$IFS" ...

  2. 一般处理程序aspx

    public bool IsReusable { get { return false; } }属性,将该属性的值改为true,为什么不起作用?按照MSDN的解释,该属性的意思是: “获取一个值,该值 ...

  3. php设计模式-单例

    单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. <设计模式>对此的定义:保证一个类仅有一个实例,并提供一个访 ...

  4. postgresql 存储过程动态插入数据 2

    最近学习postgresql,正一个小活要用上,所以就开始学习了!然而,学习的过程极其艰辛,但却也充满了乐趣. 一般来说数据库的操作不外如何增,删,改,查,而首要的就是要添加数据到数据库中,因为以前的 ...

  5. python中的random

    random.randint(a,b)    用于生成一个指定范围内的整数,a为下限,b为上限,生成的随机整数a<=n<=b;若a=b,则n=a:若a>b,报错 import ran ...

  6. 2017微软骇客马拉松精彩大回Fun:不一样的Hacker,一Young的Cool

    丹棱君有话说:一年一度激动人心的骇客马拉松大会结束了!这场内部创意大比拼硕果累累,丹棱君准备好了 6 组 Cool 骇客的别 Young 作品——沉浸式销售工具如何能守得“云”开见月明?“骇客马拉松超 ...

  7. uva 10328 - Coin Toss 投硬币(dp递推,大数)

    题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...

  8. Android(java)学习笔记138:三重for循环的优化(Java面试题)

    1. 首先我们看一段代码: for(int i=0;i<1000;i++){ for(int j=0;j<100;j++){ for(int k=0;k<10;k++){ testF ...

  9. [课堂总结]C++课堂总结(二)

    近期的面向对象程序设计的不容易记忆或者理解的东西进行一个总结,以后忘记了可以常来看下,C++是个很重要的东西,很多领域都用得到,加油,特种兵! 浅拷贝构造.深拷贝构造 浅拷贝构造是系统默认的拷贝构造函 ...

  10. Jquery二维码在线生成(不能生成图片文件)

    附件地址:http://files.cnblogs.com/files/harxingxing/jQuery%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%9C%A8%E7%BA%BF% ...