leetcode 201.Bitwise AND of Numbers Range

要求求出在一个序列中的所有数字的二进制的和,那么会发现左边相与相等即可的,则可以利用最大值的不断右移来实现的。

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int t=INT_MAX;
        while((m&t)!=(n&t)){
            t<<=1;
        }
        return m&t;
    }
};

 

posted on 2018-09-12 17:21  昔风不止,唯有努力生存  阅读(88)  评论(0编辑  收藏  举报

导航