Approach
Simple problem, for all 0, it will be zero, and for all 1, it will be 2^i-1, so we collect all the 2^i-1 within the range.
Code
class Solution {
public:
int countMonobit(int n) {
int idx = 0;
while(true) {
int cur = (1 << idx) - 1;
if(cur > n) break;
++idx;
}
return idx;
}
};