# 342.Power of Four

**342.Power of Four**

难度:Easy

> 给定一个整数 (32 位有符号整数)，请编写一个函数来判断它是否是 4 的幂次方。

```
示例 1:

输入: 16
输出: true
示例 2:

输入: 5
输出: false
进阶：
你能不使用循环或者递归来完成本题吗？
```

4的倍数的二进制末尾一定有偶数个0，前面是一个1.其实直接循环位移就可以判断，既然不使用循环，那就直接使用集合吧，32位的有符号整数，一共有16个2的整数次幂，全部写进去就好了。

```
class Solution {
    unordered_set<int> powers={1,4,16,64,256,1024,4096,16384 ,65536,262144,1048576, 4194304, 16777216,67108864, 268435456, 1073741824};
public:
    bool isPowerOfFour(int num) {
        return powers.count(num);
    }
};
```

> 执行用时 :8 ms, 在所有 C++ 提交中击败了57.68%的用户\
> 内存消耗 :9 MB, 在所有 C++ 提交中击败了5.08%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/342.power_of_four.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
