【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 example, given the range [5, 7], you should return 4.

题目大意

   给一个范围,返回这个范围中所有的数按位相与最后的结果。

解题思路

  当m!=n,那么最末位必定等0,因为[m, n]必定包含奇偶数,相与最末位等0。可以将m,n都右移一位,记为mk、 nk,这样就相当于将[m, n]之间的所有的数都右移动了一位,当mk=nk的时候,说明之前[m, n]之间的数右移一位后是相等的,右移后的数作AND操作,结果还是m(=n),所以操作就可以停止了记录右移的次数,offset,m>>offset即为所求结果

代码实现

算法实现类

public class Solution {

    public int rangeBitwiseAnd(int m, int n) {
        int offset = 0;

        while (m != n) {
            m >>= 1;
            n >>= 1;
            offset++;
        }

        return m << offset;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47997613

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 21:15:53

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

【LeetCode-面试算法经典-Java实现】【034-Search for a Range(搜索一个范围)】

[034-Search for a Range(搜索一个范围)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the targe

【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

Java for 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. 解题思路: 本题有很多思路,最简单的方法: result就是m和n二进制前面相同的部分!!! JAVA实现如下: public int ra

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. 题意:

Baozi Leetcode solution 201: Bitwise AND of Numbers Range

Problem Statement 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 Problem link Video Tutorial You can find 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 201. Bitwise AND of Numbers Range(位运算,dp)

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,那么答案就是m. 如果m<n,那么二进制最右边一位在最后的结果中肯定是0,那么就可以转化成子问题: rangeBi

201. Bitwise AND of Numbers Range java solutions

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

201. Bitwise AND of Numbers Range Leetcode Python

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 creati

[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) { /*①