Leetcode - Bitwise AND of Numbers Range

weixin_34087301發表於2016-10-15

My code:

public class Solution {
    public int rangeBitwiseAnd(int m, int n) {
        int i = 1;
        while (m != n) {
            m >>= 1;
            n >>= 1;
            i <<= 1;
        }
        
        return m * i;
    }
}

reference:
一開始看了這個最優解

https://discuss.leetcode.com/topic/12133/bit-operation-solution-java

不明白為什麼最後 m * iteration

然後看了這個:
http://www.cnblogs.com/grandyang/p/4431646.html

更加形象,就理解了。

Anyway, Good luck, Richardo! -- 10/14/2016

相關文章