题目:

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.

解答:

假如说5:101  7:111  连续几个数相与的规律:一,仅仅要是同样的位置的数字不同样最后那个位置的结果一定是0 。二,假设高位不同样,从不同样的那位到最低位都会为0,比如5和7尽管第0位同样可是因为第一位不同样,所以最后结果第0位 和第一位都为0。

假设理解了第二个规律就好办了,假设另个数位数不同样肯定最后结果为0。假设位数同样,从最高位開始寻找,将第一次发现不同样的那一位到最低位都置为0;

代码中,通过不断地右移直到两个数字相等。然后再左移同样的位数。这样做的效果事实上就是将位置不同样的都置为0

代码:

public static int rangeBitwiseAnd(int m, int n) {



int count=0;

while(m!=n)

{

m=m>>>1;

n=n>>>1;

count++;

}

return m<<count;





        

    }

LeetCode201 Bitwise AND of Numbers Range Java 题解的更多相关文章

  1. Leetcode201. Bitwise AND of Numbers Range数字范围按位与

    给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...

  2. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

  3. 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' ...

  4. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  5. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  6. 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 ...

  7. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  8. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  9. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

随机推荐

  1. BOM 之 location

    BOM 之 location它提供了与当前窗口中加载的文档有关的信息,还提供一些导航功能 .既是 window对象的属性,也是document对象的属性,就是说, window.location 和 ...

  2. React 学习资源分享 菜鸟刚学5天 博客写的不多 不懂写博客的套路

    http://www.ruanyifeng.com/blog/2015/03/react.html 首先个人强烈推荐 阮一峰的React基础 细细过一遍,看得出大师的用心良苦 然后就开始看书般的过ht ...

  3. Android DropBoxManager Service

    Android DropBoxManager Service 什么是 DropBoxManager ? Enqueues chunks of data (from various sources – ...

  4. Android 进程和文件的UID/GID

    一.文件的操作权限和UID,GID以及进程的UID,GID 1. 文件资源的权限力度:UID/GID 2. 文件的可操作权限 3. 进程的标识: PID, UID, GID, GIDs 二.UID,G ...

  5. python中的buildin函数详解(第一篇)

    这会是很长的一个帖子,因为我打算从python最基础的东西开始,尝试去完全的掌握它,buildin中有一些常用的函数比如 abs, open, setattr, getattr, 大家都很了解他们的用 ...

  6. linux平台上面python调用c

    不能免俗,先打印个helloworld出来,c代码的函数 hello.c #include <stdio.h> int helloworld() { printf("hello ...

  7. Successfully installed matplotlib

    Installing /usr/local/lib/python2.7/dist-packages/matplotlib-1.4.0-py2.7-nspkg.pthSuccessfully insta ...

  8. 对rsync进行封装的shell脚本

    抓取 #!/bin/bash . push.sh # 错误处理:尝试查找备份文件 function onError() { local errFile="err" local se ...

  9. [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】

    题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...

  10. NEURAL NETWORKS, PART 2: THE NEURON

    NEURAL NETWORKS, PART 2: THE NEURON A neuron is a very basic classifier. It takes a number of input ...