区间数字的按位与 Bitwise AND of Numbers Range

2018-08-13 22:50:51

问题描述:

问题求解:

首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0。

之后需要将m 和 n将右移一位,直到m 和 n相等。

本质上,本题就是求m 和 n的最长preSubNum。

    public int rangeBitwiseAnd(int m, int n) {
        if (m == 0) return 0;
        int moveFactor = 1;
        while (m != n) {
            m >>= 1;
            n >>= 1;
            moveFactor <<= 1;
        }
        return m * moveFactor;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9471601.html

时间: 2024-11-02 10:39:22

区间数字的按位与 Bitwise AND of Numbers Range的相关文章

[Swift]LeetCode201. 数字范围按位与 | 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 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字

【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

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(位操作)

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] 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 adding this problem and creatin

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

在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 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. 题意:

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-JAVA] 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. 思路:开始一位一位做的TLE了,这是网上很简洁很好的一个方法. 很容易理解,因为是2进制,不一样则相与为0,如果第i位一样,i-1位

[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