地址:1680. 连接连续二进制数字
结果:AC
代码:
class Solution {
public:
int concatenatedBinary(int n) {
const int max = 1000000007;
string str;
int ans = 0;
char num[32];
int i = 1;
while(i<=n){
int sheft = 31;
while( (i & (1<<sheft)) == 0) sheft--;
while(sheft >= 0){
if((i & (1<<sheft)) == 0)
str.push_back('0');
else
str.push_back('1');
sheft--;
}
i++;
}
for(int j = 0;j<str.size();j++){
ans = (ans * 2 + (str[j] - '0')) % max;
}
return ans;
}
};