题目:
Given an integer, write a function to determine if it is a power of two.
这道题和LeetCode上326题只是换了数字的差别,翻译和具体解析在这里都有介绍
http://blog.csdn.net/ZZLCSDN2017/article/details/71591345
解答:
首先计算2^30=1073741824,(都知道2^31-1是int的正数边界)
注意排非正数情况
代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0)return false;
return 1073741824%n==0;
}
};