码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 201.Bitwise AND of Numbers Range

时间:2018-09-12 18:10:20      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:style   amp   ber   while   leetcode   二进制   color   pre   span   

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

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

 

leetcode 201.Bitwise AND of Numbers Range

标签:style   amp   ber   while   leetcode   二进制   color   pre   span   

原文地址:https://www.cnblogs.com/newnoobbird/p/9636071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!