201 Bitwise AND of Numbers Range 数字范围按位与
给定范围 [m,n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含m, n两端点)。
例如,给定范围 [5,7],您应该返回 4。
详见:https://leetcode.com/problems/bitwise-and-of-numbers-range/description/
Java实现:
将m和n中的所有整数相与,得到的结果是m和n中的所有数的共同高位保留,除共同高位之外的其他位置零。那么关键问题就是如何找到这个共同的高位,其实并不难,m和n中所有数的共同高位也就是m和n的共同高位。
方法一:
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int res=0;
while(m!=n){
m>>=1;
n>>=1;
++res;
}
return (m<<res);
}
}
方法二:
class Solution {
public int rangeBitwiseAnd(int m, int n) {
while (m < n){
n &= (n - 1);
}
return n;
}
}
C++实现:
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int offset=0;
while(m!=n)
{
m>>=1;
n>>=1;
++offset;
}
return (m<<offset);
}
};
参考:https://www.cnblogs.com/grandyang/p/4431646.html
201 Bitwise AND of Numbers Range 数字范围按位与的更多相关文章
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- Leetcode201. Bitwise AND of Numbers Range数字范围按位与
给定范围 [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 ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 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 ...
- 201. Bitwise AND of Numbers Range -- 连续整数按位与的和
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
随机推荐
- Ubuntu虚拟机+ROS+Android开发环境配置笔记
Ubuntu虚拟机+ROS+Android开发环境配置笔记 虚拟机设置: 1.本地环境:Windows 7:VMWare:联网 2.虚拟环境 :Ubuntu 14.04. 比較稳定,且支持非常多ROS ...
- spring理解一
spring基本工作原理例如以下: 1.查找bean配置文件 2.载入bean配置文件并解析生成中间表示BeanDefinition 3.注冊beanDefinition 4.假设是单例或lazy-i ...
- Linux下进程信息的深入分析
这里我们主要介绍进程的状态,进程的状态可以通过/proc/PID/status来查看,也可以通过/proc/PID/stat来查看. 如果说到工具大家用的最多的ps也可以看到进程的信息.这里我们通过/ ...
- JavaSE----API之集合(Collection、List及其子类、Set及其子类、JDK1.5新特性)
5.集合类 集合类的由来: 对象用于封装特有数据,对象多了须要存储:假设对象的个数不确定.就使用集合容器进行存储. 集合容器由于内部的数据结构不同,有多种详细容器.不断的向上抽取,就形成了集合框架. ...
- Notepad++ 两个格式化插件
格式化HTML--Tidy2 本来都可以通过Notepad++中的“插件>Plugin Manager>Show Plugin Manager>Tidy2” 这种方式来安装,不过内地 ...
- AngularJS 基础入门(指令篇)
一.介绍 AngularJS 是google 开发人员设计的一个前端开发框架,它是由是由javascript 编写的一个JS框架.通常它是用来在静态网页构建动态应用不足而设计的. AngularJS特 ...
- Xcode中使用git
项目中添加git 也可在开始新建项目时勾选git,这是针对开始没有勾选git的情况 打开终端 cd 项目文件目录 //初始化一个代码仓库, git init //将当前目录及子目录中的文件标记为要添加 ...
- android中init.rc文件的解析问题
init.rc中文件里会通过import /init.${ro.hardware}.rc文件,这个ro.hardware应该是某个详细的属性.而这个ro.hardware赋值应该是在Init进程中赋值 ...
- 2016/04/13 ①html 中各种分割线------------------------------------------ ② 控制文字显示
①各种分割线Html代码 1.<HR> 2.<HR align=center width=300 color=#987cb9 SIZE=1>align 线条位置(可选left. ...
- linux CANopenSocket 初试
/************************************************************************************** * linux CANo ...