`

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.

给定一个范围,通过位与运算计算这个范围内所有的整数,返回计算后的结果。我们只需要从两个数的高位开始比较,如果遇到不相等的就停止,因为当这个范围内所有的数位与之后,只有高位相等且为1的情况下,这些位上的数才为1,其它都为0。我们借助一个help,long help = 1L << 31,让它从m和n的最高位开始比较。这里需要让help为长整型,因为int是有符号数,如果最高位为1,右移之后高位补1,而我们需要让高位补0,因此设定为长整形。代码如下:
public class Solution {
    public int rangeBitwiseAnd(int m, int n) {
        long help = 1L << 31;
        int result = 0;
        for(; help > 0; help >>= 1) {
            if((m & help) == (n & help))
                result += (n & help);
            else 
                break;
        }
        return result;
    }
}

  
0
1
分享到:
评论

相关推荐

    Leetcode 数字范围按位与.pas

    LeetCode 201的题目是"数字范围按位与"(Bitwise AND of Numbers Range),其核心要求是给定两个整数(记为m和n),需要找出这两个数范围内所有数的按位与(bitwise AND)结果。例如,给定范围[5, 7],结果应为4。 ...

    C++-Leetcode题解之201-bitwise-and-of-numbers-range.cpp

    c++ C++_Leetcode题解之201_bitwise_and_of_numbers_range.cpp

    lrucacheleetcode-RandomTasks:我为自学解决的任务

    [Bitwise AND of Numbers Range]() - 尚未上传 [LRU Cache]() - 尚未上传 [Jump Game]() - 尚未上传 []() - 尚未上传 []() - 尚未上传 []() - 尚未上传 第 5 周: []() - 尚未上传 []() - 尚未上传 []() - 尚未上传 ...

    LeetCode最全代码

    201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic.pdf

    course delves into the realm of arithmetic operations in computer systems, specifically focusing on integer representations and floating-point numbers. This chapter is crucial for understanding how ...

    计算机组成与结构体系英文课件:Chapter9 Arithmetic

    Negative numbers are represented by taking the bitwise complement of their absolute value and adding one. The leading bit, also known as the most significant bit (MSB), is 0 for positive and zero ...

    c-programming-for-scientists-and-engineers-manufacturing-engineering-series

    It is structured to cater to the needs of students pursuing various engineering and scientific disciplines, where the use of C programming can range from being a supportive topic to a core component ...

    Matters Computational-ideas, algorithms, source code

    The section on low-level algorithms covers a wide range of topics related to bit manipulation and word-level operations. Here are some of the key insights and algorithms discussed: 1. **Bit Wizardry*...

    计算机组成教学课件:Chapter9 Arithmetic

    Negative numbers are depicted by taking the bitwise complement (one's complement) of their absolute value and adding 1. For example, to represent -5 in a 4-bit two's complement system, the process ...

    python3.6.5参考手册 chm

    Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API ...

    Effective C#

    - Combine hash codes using prime numbers or bitwise operations. **Item 8: Prefer Query Syntax to Loops** - **Advantages:** Query syntax is more readable and expressive than traditional loops. - **...

    php.ini-development

    The number of significant digits displayed in floating point numbers. ; http://php.net/precision precision = 14 ; Output buffering is a mechanism for controlling how much output data ; (excluding ...

    Groovy Language Documentation Version 2.5.5

    - 其他操作符,包括展开操作符(Spread operator)、范围操作符(Range operator)、子脚本操作符(Subscript operator)、成员资格操作符(Membership operator)、身份操作符(Identity operator)、强制转换操作...

    Google C++ International Standard.pdf

    5.9 Preprocessing numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 5.10 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics