231. Power of Two

来源:互联网 发布:python 字典嵌套 list 编辑:程序博客网 时间:2024/05/16 09:37

题目

Given an integer, write a function to determine if it is a power of two.

分析

循环除2。

class Solution {public:    bool isPowerOfTwo(int n) {        if(n<=0)            return false;        if(n==1)            return true;        while(n!=2)        {            if(n%2==1)                return false;            n=n/2;        }        return true;    }};


0 0
原创粉丝点击