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 ...
随机推荐
- jira 系统服务部署(包括5.0.3版本和7.2版本)
1. 安装环境准备 1.1 安装文件下载 链接:http://pan.baidu.com/s/1i5orI9B 密码:6lih 1.2 java环境准备 2.1 jdk安装 2.2 java环 ...
- 【转载】How browsers work--Behind the scenes of modern web browsers (前端必读)
浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器- ...
- AnkhSVN介绍
AnkhSVN介绍 Posted on 2012-11-15 23:24 ArRan 阅读(3120) 评论(1) 编辑 收藏 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS ...
- Yii2 mongodb 扩展的where的条件增加大于 小于号
1. mongodb的where中有比較丰富的 条件.例如以下: static $builders = [ 'NOT' => 'buildNotCondition', 'AND' => ' ...
- Ubuntu14.04常用安装
sudo apt-get update sudo apt-get install flashplugin-nonfree ================= 类飞秋软件 sudo apt-get in ...
- Linux下用Xdebug调试php
Linux下用Xdebug调试php 博客分类: php PHPLinuxZendEclipseC# 为了调试PHP程序,安装一下xdebug. 官方网址: http://www.xdebug.org ...
- struts2多图片上传实例【转】
原文地址:http://blog.csdn.net/java_cxrs/article/details/6004144 描述: 通过struts2实现多图片上传. 我使用的版本是2.2.1,使用的包有 ...
- Hackrank Kingdom Division 树形DP
题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...
- iptraf 网卡 ip 端口 监控 netstat 关闭端口方法
18 commands to monitor network bandwidth on Linux server – BinaryTides https://www.binarytides.com/l ...
- (5)在tomcat运行自己的javaweb项目
A:在MyEclipse下方的Servers栏中启动服务器,运行项目: 1,选中项目所在的tomcat服务器 2,点击“启动按钮”,见下图 3,启动以后,看控制台输出日志: B:从服务器按钮启动: 1 ...