本文主要是介绍[LeetCode]231.Power of Two,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
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;}//ifwhile((n & 1) == 0 && n > 0) {n = n >> 1;}//whilereturn n == 1;}
};int main(){Solution s;int n = 7;bool result = s.isPowerOfTwo(n);// 输出cout<<result<<endl;return 0;
}
这篇关于[LeetCode]231.Power of Two的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!