Problem Description:
Given an integer, write a function to determine if it is a power of two.
题目大意:
给定一个整数,判断是否是2的整数幂。
Solutions:
和这道题类似,只是判断是否是2的整数幂。
Code in C++:
class Solution {
public:
bool isPowerOfTwo(int n) {
while(n>1)
{
if(n%2>0) return false;
n/=2;
}
return n==1;
}
};
近期评论