字节笔试题-转化为求公共前缀

image

利用了只要有一位为0,那么这位和多少个1还是0与操作结果都为0
还有>=2,公共前缀之后的位数一定&出来为0

而且还利用了左右移的性质去很快速的找到公共前缀

class Solution {
    public int rangeBitwiseAnd(int left, int right) {
        int count=0;
        while(left!=right){
            left=left>>1;
            right=right>>1;
            count++;
        }
        left=left<<count;
        
        return left;
    }
}
posted @ 2026-08-26 17:48  Jwwind  阅读(3)  评论(0)    收藏  举报