accepted c++ solution, 26ms


  • 0
    P
    class Solution {
    public:
        int rangeBitwiseAnd(int m, int n) {
            int res = 0;
            for (int i = 31; i >= 0; --i) {
            	int a = (m >> i) & 1;
            	int b = (n >> i) & 1;
            	if (a != b) {
            		res <<= i+1;
            		break;
            	}
            	res = res << 1 | (a&b);
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.