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.
这道题我就不说什么了,偷瞄的别人的,这么绝的解法无论如何也想不出:https://leetcode.com/discuss/35057/share-my-simple-java-solution
大概思想:如果m!=n,那么range[m,n]所有数最后一位相与必定为0,因为[m,n]必定存在奇偶数,按照此方法通过将m,n同时右移一位即可得到倒数第二位的值,依次类推。
public class Solution { public int rangeBitwiseAnd(int m, int n) { int count = 0; while(m!=n) { m = m>>1; n = n>>1; count++; } return m<<count; } }