【刷题-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.
Example 1:
Input: [5,7]
Output: 4
Example 2:
Input: [0,1]
Output: 0
解法1 一个一个做位与,但是会超时。。。
解法2 事实:
- 与运算中,只要有一个是0,结果肯定是0
- 将二进制数字看作是字符串时,m和n的共同前缀也是范围[m, n]中所有数字的共同前缀
因此只需要求出m和n的共同前缀即可
解法2.1 移位。将m和n逐一向右移位,直到两者相等
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int shift = 0;
while(m != n){
m >>= 1;
n >>= 1;
shift++;
}
return m << shift;
}
};
解法2.2 Brian Kernighan's Algorithm。n&(n-1)会将n中最右边的1翻转为0
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
while(m < n)n &= n - 1;
return m & n;
}
};
【刷题-LeetCode】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 ...
- 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 201 Bitwise AND of Numbers Range 位运算 难度:0
https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...
- 【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】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
随机推荐
- 七层LB-NGINX 客户端获取协议Proxy Protocol介绍
协议诞生背景 在Web分布式系统中,一般会搭建复杂的load blance系统来提供高性能的web服务. 7层的SLB 有基于nginx/淘宝变种tengine的.后端RS会无法获取真实客户端IP. ...
- libevent 源码分析
1,前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libevent代码和设计思想做系统的.更深层次的分析, ...
- 查找MySql的配置文件my.cnf所在路径
Linux系统 linux 上可以使用 mysql --help|grep my.cnf 过滤查看 [root@localhost etc]# mysql --help|grep my.cnf ord ...
- Java的运行机制
Java程序运行机制 编译型(操作系统 c/c++) 解释型(网页 不追求速度) 程序运行机制
- VS2010 sp1离线下载地址和在线下载地址
地址是:http://www.msdn1.cn/8/42/ 下载: edk2 + 迅雷, 稳的1P
- 1246 - Colorful Board
1246 - Colorful Board PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- AlexNet: ImageNet Classification with Deep Convolutional Neural Networks
目录 激活函数 防止过拟合 增加数据 Dropout 细节 代码 AlexNet 上图是论文的网络的结构图,包括5个卷积层和3个全连接层,作者还特别强调,depth的重要性,少一层结果就会变差,所以这 ...
- <数据结构>XDOJ332.二叉排序树的判定
问题与解答 问题描述 给定一个二叉树,判断其是否是一个有效的二叉排序树. 假设一个二叉排序树具有如下特征: 结点的左子树只包含小于当前结点的树. 结点的右子树只包含大于当前结点的树. 所有左子树和右子 ...
- MySQL高级查询与编程作业目录 (作业笔记)
MySQL高级查询与编程笔记 • [目录] 第1章 数据库设计原理与实战 >>> 第2章 数据定义和操作 >>> 2.1.4 使用 DDL 语句分别创建仓库表.供应 ...
- 编写Java程序,实现从控制台输入对应个数的整数,输出对输入整数的从大到小显示
编写Java程序,实现从控制台输入对应个数的整数,输出对输入整数的从大到小显示 效果如下: 实现代码: import java.util.Arrays; import java.util.Scanne ...