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 ...
随机推荐
- 067 Add Binary 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示).案例:a = "11"b = "1"返回 "100" .详见:https://leetc ...
- android模拟器创建时的PANIC: Could not open:错误的解决
创建AVD之后,在启动时报如下错误,解决方法如下: 在环境变量中创建ANDROID_SDK_HOME=D:\Program Files (x86)\Android\android-sdk,后面的当然是 ...
- sqlsever 判断某个字段出现重复的字母或字符
-------下面使用标量值函数判断 出现重复的个数 create function fn_str_times(@str varchar(1000),--原子符串@indexstr varchar( ...
- SpringBoot 2.x (15):Actuator监控
Actuator监控:SpringBoot自带的,对生成环境进行监控的系统 使用:既然是监控,那就不能监控一个空项目 这里我使用SpringBoot整合MyBatis的Demo: https://ww ...
- 模态框的理解 ,jQ: loading,进度条, 省级联动 表单验证 插件
模态框: 打开一个弹框 不关闭它就不能做框外的操作 必须关闭或弹出另外的弹框 加载延迟loading + 进度条只要有请求 就处理一下监控ajax 全局事件jquery: $('#box').ajax ...
- CF1166C A Tale of Two Lands
思路: 搞了半天发现和绝对值无关. http://codeforces.com/blog/entry/67081 实现: #include <bits/stdc++.h> using na ...
- ABAP跳转屏幕
1.call transaction语句跳转屏幕 '. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. . 2.调用函数 CALL FUNCTION 'M ...
- 在使用添加按钮给table插入新的一行时遇见的问题总结及处理方法
添加按钮的功能:点击添加按钮之后完成添加新的一行. 遇见的问题:当多次点击添加按钮生成新的多行之后,生成的每行内部按钮的保存按钮点击事件出现最晚添加的一行的行内保存点击事件执行一次,倒数第二次添加的行 ...
- CentOS7.2上安装Python3.6
CentOS 7下安装Python3.6 1)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-devel gdbm-d ...
- javaSe-SimpleDateFormat
SimpleDateFormat呢是一种可以将字符串转为日期或者日期转换成字符串的功能强大的不得了的类: import java.text.ParseException;import java.tex ...