Given an integer, write a function to determine if it is a power of two.
class Solution {
public:
bool isPowerOfTwo(int n) {
return n < 1 ? false : (n | (n-1)) == n * 2 - 1;
}
};
Given an integer, write a function to determine if it is a power of two.
class Solution {
public:
bool isPowerOfTwo(int n) {
return n < 1 ? false : (n | (n-1)) == n * 2 - 1;
}
};
本文标题:231. Power of Two
本文链接:https://www.haomeiwen.com/subject/zloeettx.html
网友评论