Leetcode - Bitwise AND of Numbers Range

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

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/1165b1e73e86
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞