题目
Given an integer, write a function to determine if it is a power of two.
代码
/*---------------------------------------
* 日期:2015-08-02
* 作者:SJF0115
* 题目: 231.Power of Two
* 网址:https://leetcode.com/problems/power-of-two/
* 结果:AC
* 来源:LeetCode
* 博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0){
return false;
}//if
while((n & 1) == 0 && n > 0) {
n = n >> 1;
}//while
return n == 1;
}
};
int main(){
Solution s;
int n = 7;
bool result = s.isPowerOfTwo(n);
// 输出
cout<<result<<endl;
return 0;
}
时间: 2024-09-30 21:59:11