https://leetcode.com/problems/power-of-two/
Given an integer, write a function to determine if it is a power of two.
class Solution {
public:
bool isPowerOfTwo(int n) {
return (n>0) && !(n&(n-1));
}
};
判断整数是否为2的幂次
本文介绍了一个简洁的算法,用于判断一个给定的整数是否为2的幂次。通过位运算的方式,该算法实现了高效且易于理解的解决方案。
https://leetcode.com/problems/power-of-two/
Given an integer, write a function to determine if it is a power of two.
class Solution {
public:
bool isPowerOfTwo(int n) {
return (n>0) && !(n&(n-1));
}
};
被折叠的 条评论
为什么被折叠?