LeetCode 0231 - Power of Two

2021-08-11 14:56:46 浏览数 (1)

Power of Two

Desicription

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

Example 1:

代码语言:javascript复制
Input: 1
Output: true 
Explanation: 20 = 1

Example 2:

代码语言:javascript复制
Input: 16
Output: true
Explanation: 24 = 16

Example 3:

代码语言:javascript复制
Input: 218
Output: false

Solution

代码语言:javascript复制
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0) {
            return false;
        } else {
            return !(n&(n-1));
        }
    }
};

0 人点赞