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 ...
随机推荐
- vue $emit、$on、$refs简介
1.$emit 触发当前实例上的事件.附加参数都会传给监听器回调 ex: 子组件调用父组件的方法并传递数据注意:子组件标签中的时间也不区分大小写要用“-”隔开 子组件: <template> ...
- python - django 实现文件下载功能
使用 Django 搭建的网站中添加一个可以从服务器中下载文件的功能 (此处演示一个从网站中下载API文档的例子供参考) # 一.url urlpatterns = [ # 下载 API 接口文档 ...
- Django 中使用redis
Django使用redis 方式一,使用Django-redis模块 #安装: pip3 install django-redis CACHES = { "default": ...
- JSON和计算机网络的个人总结
JSON JSON是什么? JSON:JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于ECMAScript (欧洲计算机协会制定的js规范) ...
- (实例2) TFT2.0液晶屏幕测试 mega2560
关键修改 针对mega2560板子 #define TFT_RST A4 #define TFT_RS A3 #define TFT_CS A5 // SS #define TFT_SDI A2 // ...
- codevs 1814 最长链题解
codevs 1814 最长链题解 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Descripti ...
- 61、Spark Streaming:部署、升级和监控应用程序
一.部署应用程序 1.流程 1.有一个集群资源管理器,比如standalone模式下的Spark集群,Yarn模式下的Yarn集群等. 2.打包应用程序为一个jar包. 3.为executor配置充足 ...
- pip崩了, 解决 ModuleNotFoundError: No module named 'pip'.
今天 在windows下用pip 安装数据库模块pymysql 把pip 弄崩了,直接出现下面的错误.都是红字, 再输入pip install pymysql ,会报错ModuleNotFound ...
- VSCode 本地如何查看历史页面
1.首先要在VSCode的扩展中安装一个 Local history插件,蓝色框部分不用管,直接安装即可 2.安装并操作:安装后,修改 productManage/supplierList/addSu ...
- 【pytorch 代码】pytorch 网络结构可视化
部分内容转载自 http://blog.csdn.net/GYGuo95/article/details/78821617,在此表示由衷感谢. 此方法需要安装python-graphviz: con ...