class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
return (n & (n - 1)) == 0;
}
};
本文介绍了一种高效的方法来判断一个整数是否可以表示为2的幂次方。通过位运算的方式,可以在O(1)的时间复杂度内完成判断,这种方法对于编程面试和技术挑战非常有用。
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) return false;
return (n & (n - 1)) == 0;
}
};
6185
6161

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