Last active
May 3, 2022 11:58
-
-
Save thekhairul/996a3e7ef97a1ad420113a41703660bc to your computer and use it in GitHub Desktop.
Leetcode 231: Power of Two - python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isPowerOfTwo(n): | |
left = 0 | |
right = 31 | |
result = False | |
while left <= right: | |
mid = (left + right) // 2 | |
if pow(2, mid) == n: | |
result = True | |
break | |
if pow(2, mid) < n: | |
left = mid + 1 | |
else: | |
right = mid - 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment