`
SunnyYoona
  • 浏览: 361716 次
社区版块
存档分类
最新评论

[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;
        }//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;
}
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics