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

思路:

由于相邻的两个数最低位肯定有0有1,所以直接and肯定为0,所以可以通过直接and来和向右移位获得一个区间内的相同的位数,最后再通过向左移位获得一个区间所有数字相与的结果。

代码:

 public int rangeBitwiseAnd(int m, int n) {
        int offset=0;
        while(m!=n)
        {
            m>>=1;
            n>>=1;
            offset++;
        }
        return m<<offset;
    }
时间: 2024-08-07 07:31:54

leetcode_Bitwise AND of Numbers Range的相关文章

【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. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for a

leetcode_201题——Bitwise AND of Numbers Range(位操作)

Bitwise AND of Numbers Range Total Accepted: 9698 Total Submissions: 35771My Submissions Question Solution Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given th

【LeetCode-面试算法经典-Java实现】【201-Bitwise AND of Numbers Range(范围数位与结果)】

[201-Bitwise AND of Numbers Range(范围数位与结果)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For exa

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' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so t

[LeetCode#201] Bitwise AND of Numbers Range

Problem: 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. Analysis: The idea behind this problem is not hard, you could

【leetcode】Bitwise AND of Numbers Range(middle)

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. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经

[leedcode 201] 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. public class Solution { public int rangeBitwiseAnd(int m, int n) { /*①

LeetCode201 Bitwise AND of Numbers Range Java 题解

题目: 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 .二,如

【LeetCode 201】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. 题意: 给定 [m, n] 范围内个数,返回范围内所有数相与的结果. 思路: 如果打着暴力的旗号,那么这个题是会超时的 - -!.最后也是没