LeetCode(201) 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.
分析
题目字面意思是给定两个整数构成闭区间, 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;
}
};
LeetCode(201) Bitwise AND of Numbers Range的更多相关文章
- 【LeetCode 201】Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
随机推荐
- Win10专业版系统下添加其他国家语言
Win10专业版系统下如何添加其他国家语言?国内的win10专业版系统默认情况下是安装简体中文,但是有的用户出于工作原因需要使用其它字体.比如外国友人就需要使用英语,西班牙等.其实win10专业版是支 ...
- NET Core:部署项目到Ubuntu Server
NET Core:部署项目到Ubuntu Server 概述 基于上一篇成功安装Ubuntu Server 16.10的基础上,接下来继续我们ASP.NET Core项目的部署之旅! 只是对于这些年整 ...
- mysql issue:
####0 https://yq.aliyun.com/ziliao/53466 首先,很荣幸你找到了这篇文章... 如果你忘记了mysql的密码不妨试试以下这个方法. 1.打开my.cnf 代码如 ...
- [Oracle 视图] ALL_OBJECTS
ALL_OBJECTS ALL_OBJECTS describes all objects accessible to the current user. ALL_OBJECTS描述当前用户的可访问的 ...
- 常用浏览器User-Agent大全
=======================PC浏览器======================== OperaMozilla/5.0 (Windows NT 6.1; WOW64) AppleW ...
- LeetCode Happy Number 开心数字
题意: 给出一个整数n,判断其是否为幸运数. 规则是,将n按十进制逐位拆出来后,每个位各自进行取平方,再将这些平方数求和作为新的数字n.若最后n=1,就是幸运数. 思路: 计算例子:n=47,接着n= ...
- C# 生成条形码图片
在网上看到一些人写关于条形码的代码都很长,有的甚至拿来卖,所以查了下资料,希望能对大家有帮助. 我的实现原理是: 其实Windows本身就有一个字体是用来显示条形码的. 只要将数字改为这种字体就变成了 ...
- CCCC 以及 hihocoder offer收割赛11 ~~~
CCCC 真的很蒙 ,没有队服,没有狗牌,服务器崩溃到14:10 才开始比赛...(黑人问号 开始前,发现旁边是西交老大吴航,mad~各种紧张.看着大佬疯狂的敲宏定义就很怕啊.100多行,一行头 ...
- [神经网络]一步一步使用Mobile-Net完成视觉识别(二)
1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第二篇,调用官方例子并获取数据集. 上一节里面记得我们需要配置PYTHONPATH,大家应该发现,每次 ...
- mangoDB笔记
1. 查询 db.表.find().pretty() find(querry,project) pretty()格式化显示 findOne() 返回一条结果 比较 db.Decl_In.find( ...