# 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; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up