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.
For example, given the range [5, 7], you should return 4.
分析:http://www.cnblogs.com/grandyang/p/4431646.html
我们先从题目中给的例子来分析,[5, 7]里共有三个数字,分别写出它们的二进制为:
101 110 111
相与后的结果为100,仔细观察我们可以得出,最后的数是该数字范围内所有的数的左边共同的部分,如果上面那个例子不太明显,我们再来看一个范围[26, 30],它们的二进制如下:
11010 11011 11100 11101 11110
- public class Solution {
- int rangeBitwiseAnd(int m, int n) {
- int i = ;
- while (m != n) {
- m >>= ;
- n >>= ;
- ++i;
- }
- return (m << i);
- }
- }
Bitwise AND of Numbers Range的更多相关文章
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- [leetcode] Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 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. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 【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 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 ...
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
随机推荐
- offsetLeft, offsetTop以及postion().left , postion().top有神马区别
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java算法-符号&
&与运算符 与运算符用符号“&”表示,其使用规律如下:两个操作数中位都为1,结果才为1,否则结果为0 例如下面的程序段. public class data13 { public s ...
- 你也可以当面霸-Servlet与JSP的原理及特点
既然是面试系列,就是面试官和应聘者之间的对话.本文是采用一问一答的形式呈现给读者的,这样能有一个明确的考察点,不像理论知识那么枯燥. 01.什么是Servlet技术 Servlet是和平台无关的服务器 ...
- 洛谷P2327 [SCOI2005] 扫雷
题目描述 输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二列的格子中的数.(1<= N <= 10000) 输出格式: 一个数,即第一列中雷的摆放方案数. 输入输出样例 输 ...
- 使用Java的嵌套循环打印出平行四边形、等腰三角形、棱形、矩形的星星图案(Java工程师面试必备)
第一遍是看了视频,听老师讲解嵌套循环的使用,然后到星星图形这一步,当时都觉得听明白了,但是自己去做,就是写不出来 第二遍看了赵老师的教程,看了好熟悉的感觉,还是自己写不出来 第三遍找网上关于图形的嵌套 ...
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- JS 瀑布流布局
瀑布流布局 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- express快速搭建web server
安装express4.x npm install -g express npm install -g express-generator //express命令行工具在4.x分离出来了 express ...
- Android往SD卡上存储文件
public class DataActivity extends Activity { private EditText filenameText; private EditText content ...
- 如何查看LINUX 硬件配置信息
如何查看LINUX 硬件配置信息 在网上找了N久,发现了一篇不错的文档,转载一下: 1.查看机器所有硬件信息: dmidecode |more dmesg |more 这2个命令出来的信息都非常多,所 ...