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.

StackOverflow:http://math.stackexchange.com/questions/1073532/how-to-find-bitwise-and-of-all-numbers-for-a-given-range

寻找最高位m和n都是1的值既是所要的。但是同时所有低位值都要被清零。

public class Solution {
  public int rangeBitwiseAnd(int m, int n) {
    int a = m^n;
    int s = a >> 1;
    while (s != 0) {
      a |= s;
      s >>= 1;
    }
    return m&n&~a;
  }
}

时间: 2024-08-26 14:55:23

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

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

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

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

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

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

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