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 the detailed video tutorial here

Thought Process

Normally we just implement as we do at work, which is keep increasing the number and do an AND. It will still pass the OJ, just need to pay attention to overflow situation (using a long would solve the problem)

Now we are pushing ourselves, can we solve it more than linear. Only log/binary is faster than linear. The idea is to find the common left bits of m and n, and later shift n (total number of digits - common length digits) because the right part would end up to be 0.

For example, from 4 to 7, thte common left part is 1, the range and value would be 100 (which is n left shift twice)

  • 1 00
  • 1 01
  • 1 10
  • 1 11

Solutions

Linear solution

 1 public int rangeBitwiseAnd(int m, int n) {
 2     // avoid overflow
 3     long res = m;
 4
 5     for (long i = (long)m + 1; i <= (long)n; i++) {
 6         res = res & i;
 7         if (res == 0) return 0;
 8     }
 9
10     return (int)res;
11 }

Time Complexity: O(N) essentially n - m

Space Complexity: O(1) no extra space is needed

Logarithmic solution

 1 public int rangeBitwiseAnd(int m, int n) {
 2     if (n == m) {
 3         return n;
 4     }
 5     int digit = 0;
 6     while (m != n) {
 7         m >>= 1;
 8         n >>= 1;
 9         digit++;
10     }
11
12     return m << digit;
13 }

Time Complexity: O(lgN) because we keep dividing 2 (left shift) of n

Space Complexity: O(1) no extra space is needed

References

原文地址:https://www.cnblogs.com/baozitraining/p/11614656.html

时间: 2024-11-03 21:38:56

Baozi Leetcode solution 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

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

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

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-f

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

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