我可以说我用70分钟做完所有然后ac全部吗?第一次遇到这么顺利的题,第二题二进制1的是程序员面试金典的题,第一题和第三题竟然直接手写没调试即ac
class CloseNumber {
public:
vector<int> getCloseNumber(int x) {
// write code here
int i, j;
int small, big;
for(i = 0; i < 32 && (x & (1 << i)) != 0; ++i) ;
if(i > 0)
{
big = x + (1 << (i - 1));
for(j = i ; j < 32 && (x & (1 << j)) == 0; ++j) ;
//small = x - (j << (j - 1));
small = x & ~((1 << (j + 1)) - 1);
for(int k = 0; k <= i; ++k)
small |= (1 << (j - k - 1));
}
else
{
for( ; i < 32 && (x & (1 << i)) == 0; ++i) ;
for(j = i; j < 32 && (x & (1 << j)) != 0; ++j) ;
big = x + (1 << i);
for(int k = 0; k < j - i - 1; ++k)
big |= (1 << k);
small = x - (1 << (i - 1));
}
return vector<int>({small, big});
}
};