美文网首页Leetcode, again
Leetcode - Bitwise AND of Number

Leetcode - Bitwise AND of Number

作者: Richardo92 | 来源:发表于2016-10-15 00:30 被阅读8次

    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

    相关文章

      网友评论

        本文标题:Leetcode - Bitwise AND of Number

        本文链接:https://www.haomeiwen.com/subject/wounyttx.html