Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.
Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).
public int rangeBitwiseAnd(int m, int n) { while (n > m) n &= (n-1); return n; }
n & (n-1) turns off the right most bit.
n & (n-1)
Here's more to it on stackoverflow-kerninghan..
I am sorry, that's not one line Java Solution :P
public int rangeBitwiseAnd(int m, int n) { return n > m ? rangeBitwiseAnd(m, n & (n - 1)) : n; }
Can you explain why this algorithm works?
He just rewrote the algorithm above in recursive way.
Can you explain it in detail?
@jaqenhgar Can you please tell what will be the time complexity of the algorithm, and also it's working. Thanks!
@SanD Directly it will be O(1) since there are at most 32 bits for an integer.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.