题目

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. php时间戳存在8小时误差

    当将PHP时间戳转化为正常的时间格式一般的操作方法如下: $mytime=time(); echo $mytime.'<br />'; echo date('Y-m-d H:i:s',$m ...

  2. thymeleaf中th:attr用法以及相关的thymeleaf基本表达式

    额,有人写的很好,我直接搬了 thymeleaf中th:attr用法 1.写死的单个属性值添加 th:attr="class=btn" 2.写死的多个属性值添加 th:attr=& ...

  3. 提升Java代码质量(一)

    博主双12入手了一本"Effective Java第二版",本系列文章将初步梳理书中内容,我也查了些资料,我会针对知识点做一点展开,方便以后复习回顾; Item1.考虑用静态工厂代 ...

  4. SharePoint Server和Office 365之间的混合模式集成概述

    正如您可能已经知道的那样,云中的Microsoft Office 365和SharePoint Server 2013/2016内部部署可以通过多种方式协同工作.这些通常被称为混合模式,因为它们将功能 ...

  5. 首次将项目从svn下载到eclipse

    1.点击 File --> Import,进入导入项目窗口 2.选择从SVN检出项目,点击Next 3.选择创建新的资源库位置,点击Next 4.在URL处输入SVN项目远程地址,点击Next ...

  6. jsp另外五大内置对象之response-操作cookie

    responseo3.jsp <%@ page language="java" contentType="text/html; charset=utf-8" ...

  7. POJ 1067 取石子游戏 (威佐夫博奕,公式)

    题意: 有两堆石子,两个人轮流取石子.规定每次有两种取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者.给定两堆石子数量,问先手的输赢? ...

  8. width:100%与绝对定位同时存在,偏移出父级容器

    当父级容器内的子元素width设为100%,而子元素又有绝对定位时,子元素伸展超出父级容器,像下面 出现这种情况的原因,width:100%,这个百分之百是相对其定位父级而言的,其定位父级有多宽,这个 ...

  9. UVA 110020 Efficient Solutions (STL)

    把一个人看出一个二维的点,优势的点就是就原点为左下角,这个点为右上角的矩形,包含除了右上角以外边界,其他任意地方不存在点. 那么所有有优势的点将会形成一条下凹的曲线. 因为可能有重点,用multise ...

  10. What is a meta-class in Objective-C?

    http://www.cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html In this post, I look at ...