class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t result = 0;
for(int i=0; i<32; i++){
if(n & 1 == 1){
result = (result << 1) + 1;
}
else{
result = result << 1;
}
n = n >> 1;
}
return result;
}
};
如果需要翻转的数的最后一位是1,则result左移一位,加一。否则只左移一位。最后将n右移一位。
本文提供了一个使用C++编程语言解决32位整数翻转问题的解决方案,通过定义一个名为classSolution的类,其中包含一个名为reverseBits的函数,该函数实现了将输入整数的每一位进行翻转的功能。
694

被折叠的 条评论
为什么被折叠?



