【LeetCode】201. Bitwise AND of Numbers Range
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.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
从m到n逐个做与操作肯定是不合理的,最多要进行INT_MAX*32次位与操作。
可以把复杂度降低到32次移位并处理。
对于每一位来说,只要中间存在一个0,该位就是0,只有全1的时候才是1.
因此问题变为:对于从右数第i位数字,从m到n之间是否全1?
满足全1要同时满足两个条件:
(1)m的相应位置是1 即起始位置必须是1
(2)m到n之间的间隔不大于m到最后一个连续1的间隔
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int ret = ;
int gap = n - m;
if(gap == )
return m;
int bit = ;
//note that 0 <= m <= n <= 2147483647
//the highest bit must be 0, thus skip i == 31
for(int i = ; i < ; i ++)
{//bit by bit check zero
int ind = m % (int)pow(2.0, i+);
if((ind >= (int)pow(2.0, i)) && (ind+gap <= (int)pow(2.0, i+)-))
//all 1's
ret |= bit;
bit <<= ;
}
return ret;
}
};
【LeetCode】201. Bitwise AND of Numbers Range的更多相关文章
- 【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
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- Java for 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#201] Bitwise AND of Numbers Range
Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...
- leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 【LeetCode】898. Bitwise ORs of Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
随机推荐
- 使用Java语言开发微信公众平台(三)——被关注回复与关键词回复
在上一篇文章中,我们实现了文本消息的接收与响应.可以在用户发送任何内容的时候,回复一段固定的文字.本章节中,我们将对上一章节的代码进行适当的完善,同时实现[被关注回复与关键词回复]功能. 一.微信可提 ...
- git学习一二三一
svn用的多,但是我是一个geek,git这个美丽的scm,我怎能错过了?于是最近在全方位的窥视它的酮体,把我的一点心得分享给大家把. 先说一说给git的历史, Git是一个开源的分布式版本控制系统, ...
- Android -- Camera2(Android5.0)
Camera2 Camera2是Android5.0中的其中一个新的特性,新的API.与原来的camera API相比,不同之处在于: 原生支持RAW照片输出 突发拍摄模式 制约拍照速度的不再是软件而 ...
- javascript——select 标签的使用
<% String state = (String) request.getAttribute("state"); String day = (String) request ...
- Redis实战总结-Redis的高可用性
在之前的博客<Redis实战总结-配置.持久化.复制>给出了一种Redis主从复制机制,简单地实现了Redis高可用.然后,如果Master服务器宕机,会导致整个Redis瘫痪,这种方式的 ...
- Springboot单元测试(MockBean||SpyBean)
转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...
- ios 开发 ping
在win 或 mac系统的终端下都有方便的ping命令来检测网络的连通性! 在iOS上可以使用苹果封装好的工具来开发ping连接测试 SimplePing 下载 1. 主要接口 //初始化一个地址 - ...
- 微信小程序 - 使用字体图标(阿里云库)
实现 阿里图库:https://www.iconfont.cn/ CSS3:[attribute*=value] 选择器 步骤 1. 加入购物车 2. 下载代码解压,复制iconfont.css ap ...
- 微信小程序 - template和include详细描述
演示 index.wxml <!-- 内部模板 --> <template name="all"> {{a}} {{b}} </template> ...
- .NET MVC中登陆授权过滤器的使用
1.写个类LoginAuthorityAttribute,继承自AuthorizeAttribute using System; using System.Collections.Generic; u ...