---
# System prepended metadata

title: LeetCode 231. Power of Two

---

# LeetCode 231. Power of Two
[LeetCode 231. Power of Two](https://leetcode.com/problems/power-of-two) (難度 通過率)
<!-- 
(<font color=#00AF9B>Easy</font> 53.8%) 
(<font color=#FFB800>Medium</font> 39.6%)
(<font color=#FF375F>Hard</font>)
 -->
    
- 限制 :
    <ul>
    <li><code>-2^31 <= n <= 2^31 - 1</code></li>
    </ul>

- Solution
    這題就... 找一下而已，應該不難XDD
    - 時間複雜度: $O(log(n))$
    - 空間複雜度: $O(1)$

- 程式碼
    ```c++=
    class Solution {
    public:
        bool isPowerOfTwo(int n) {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);

            if (n <= 0)
                return false;
            while (n > 1) {
                if (n % 2)
                    return false;
                n /= 2;
            }
            return true;
        }
    };
    ```
