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 ...
随机推荐
- .NET Core模块化
.NET Core模块化 源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com账户:ad ...
- AD中添加中文字符丝印的方法:
一 一般中文丝印: 用快捷键L打开层管理,在View options中勾选convert special 选项: 用快捷键P,S文本中输入你要的汉字,选中ture type,在select ture ...
- Windows7&IIS7.5部署Discuz全攻略
组长说在内网部署一个论坛,这可难不倒我,装个Discuz嘛.部署环境就一台普通的PC,四核i3,Windows7.这就开搞了. 准备工作 系统是Windows 7 专业版,自带IIS7.5(家庭版不带 ...
- DialogHelper
//require ScrollHelper.js function DialogHelper() { var _this = this; var doc = window.document; _th ...
- Php—AJAX跨域问题
<?php /** * ajax proxy * ajax跨域解决办法 * @author suconghou <suconghou@126.com> * @version v1. ...
- 高效的设计可视化UI
http://www.uimaker.com/uimakerdown/uitutorial/35990.html http://maqetta.org/downloads/ .Data.js Data ...
- 织梦dedecms手机版上下篇链接错误的解决方法
打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...
- 宿主机Windows访问虚拟机Linux文件(二)
上一篇文章中详细讲述FTP服务(基于文件传输协议的服务),本文则介绍另一种能够实现此功能Telnet(Telecommunications network 远程登陆)服务.本文介绍的telnet我常用 ...
- HDU 5459 Jesus Is Here (递推,组合数学)
有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...
- SpringMVC-请求参数的绑定
绑定的机制 表单提交的数据都是k=v格式的 username=haha&password=123 SpringMVC的参数绑定过程是把表单提交的请求参数,作为控制器中方法的参数进行绑定的 要求 ...