LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与
201. Bitwise AND of Numbers Range
题目描述
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
LeetCode201. Bitwise AND of Numbers Range中等
示例 1:
输出: 4
示例 2:
输出: 0
Java 实现
方法一
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int d = Integer.MAX_VALUE;
while ((m & d) != (n & d)) {
d <<= 1;
}
return m & d;
}
}
方法二
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int count = 0;
while (m != n) {
m >>= 1;
n >>= 1;
count++;
}
return m << count;
}
}
参考资料
- https://leetcode.com/problems/bitwise-and-of-numbers-range/
- https://www.cnblogs.com/grandyang/p/4431646.html
- https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/
LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)的更多相关文章
- C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解
C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...
- [Swift]LeetCode201. 数字范围按位与 | Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 区间数字的按位与 Bitwise AND of Numbers Range
2018-08-13 22:50:51 问题描述: 问题求解: 首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0. 之后需要将m 和 n将右移一位,直到m 和 n相等. 本质上, ...
- Java实现 LeetCode 201 数字范围按位与
201. 数字范围按位与 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入 ...
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...
- leetcode 201. 数字范围按位与 解题报告
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...
- 【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 ...
- 【刷题-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 1102. Path With Maximum Minimum Value
原题链接在这里:https://leetcode.com/problems/path-with-maximum-minimum-value/ 题目: Given a matrix of integer ...
- Windows 2008R2 安装PostgreSQL 11.6
前些天在CentOS 7.5 下安装了PostgreSQL 11.6.除了在无外网环境下需要另外配置之外,其他没有什么差别.今天主要写一下在Windows下面安装PostgreSQL的问题. 在官网看 ...
- vant - Navbar slot 插槽使用
//子组件 <template> <van-nav-bar> <slot slot="left" name="left">& ...
- ML.NET 笔记
ROC曲线 ROC空间将偽陽性率(FPR)定義為 X 軸,真陽性率(TPR)定义为 Y 轴. TPR:在所有實際為陽性的樣本中,被正確地判斷為陽性之比率. FPR:在所有實際為阴性的样本中,被錯誤地判 ...
- High scalability with Fanout and Fastly
转自:http://blog.fanout.io/2017/11/15/high-scalability-fanout-fastly/ Fanout Cloud is for high scale d ...
- 开发框架Express
一.使用原因 由于nodejs原生的http核心模块在某些方面不足以应对开发需求,所以就需要使用框架来加快开发效率,让代码更高度统一.在nodejs中有许多web开发框架,以下介绍Express的使用 ...
- 用pickle保存机器学习模型
在机器学习中,当确定好一个模型后,我们需要将它保存下来,这样当新数据出现时,我们能够调出这个模型来对新数据进行预测.同时这些新数据将被作为历史数据保存起来,经过一段周期后,使用更新的历史数据再次训练, ...
- 海底高铁(洛谷 P3406)
题目背景 大东亚海底隧道连接着厦门.新北.博艾.那霸.鹿儿岛等城市,横穿东海,耗资1000亿博艾元,历时15年,于公元2058年建成.凭借该隧道,从厦门可以乘坐火车直达台湾.博艾和日本,全程只需要4个 ...
- 思科ACL阻止勒索病毒
如何在思科的3700系列交换机上配置ACL阻止勒索病毒的传播? 勒索病毒主要是微软的锅,通过TCP/UDP的135.137.138.139.445端口攻陷用户的计算机加密用户的文件达到勒索比特币的目的 ...
- JSP的工作原理
jsp的本质就是一个servlet,jsp在第一次被访问时会被Web容器翻译成servlet index.jsp -> index_jsp.java -> 编译成index_jsp.cla ...