您的位置:首页 > 其它

[leetcode] Power of Two

2015-07-14 14:24 218 查看
From :https://leetcode.com/problems/power-of-two/

Given an integer, write a function to determine if it is a power of two.
Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Hide Tags
Math Bit
Manipulation

Hide Similar Problems
(E) Number of 1 Bits

Solution :

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) return false;
long long k=1;
while(k<n) {
k = k<<1;
}
return n==k;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: