aboutsummaryrefslogtreecommitdiff
path: root/power_of_two.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /power_of_two.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'power_of_two.py')
-rw-r--r--power_of_two.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/power_of_two.py b/power_of_two.py
new file mode 100644
index 0000000..80dd7d0
--- /dev/null
+++ b/power_of_two.py
@@ -0,0 +1,24 @@
+# 231. Power of Two
+
+"""
+given an integer 'n', return true if it is a power of two. otherwise, return
+false. an integer 'n' is a power of two if there exists an integer 'x' such
+that 'n == 2^x'
+"""
+
+
+class Solution(object):
+ def isPowerOfTwo(self, n):
+ """
+ :type n: int
+ :rtype: bool
+ """
+ if n <= 0:
+ return False
+ return not (n & (n - 1))
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isPowerOfTwo(1))
+ print(obj.isPowerOfTwo(16))
+ print(obj.isPowerOfTwo(3))