leetCode(32):Power of Two

来源:互联网 发布:mysql语句入门 编辑:程序博客网 时间:2024/04/29 05:45

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

       2的幂的二进制表示中,必然只有一个“1”,且不可能为负数。

class Solution {public:    bool isPowerOfTwo(int n) {        if(n<0)        {//若为负数则直接返回        return false;        }    int num=0;    while(n)    {//统计1的个数    n=n&(n-1);    num++;    }    if(num==1)    return true;    return false;    }};


0 0
原创粉丝点击