From b21c4ec67fcba793eec9f05b05f8a944f7112bec Mon Sep 17 00:00:00 2001 From: Mohammad Shakib <50780268+Mo-Shakib@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:54:26 +0600 Subject: [PATCH] Accepted --- easy/231-power-of-two.py | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 easy/231-power-of-two.py diff --git a/easy/231-power-of-two.py b/easy/231-power-of-two.py new file mode 100644 index 0000000..4f3dc82 --- /dev/null +++ b/easy/231-power-of-two.py @@ -0,0 +1,75 @@ +# +# @lc app=leetcode id=231 lang=python3 +# +# [231] Power of Two +# +# https://leetcode.com/problems/power-of-two/description/ +# +# algorithms +# Easy (48.04%) +# Likes: 6958 +# Dislikes: 447 +# Total Accepted: 1.5M +# Total Submissions: 3.1M +# Testcase Example: '1' +# +# 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. +# +# +# Example 1: +# +# +# Input: n = 1 +# Output: true +# Explanation: 2^0 = 1 +# +# +# Example 2: +# +# +# Input: n = 16 +# Output: true +# Explanation: 2^4 = 16 +# +# +# Example 3: +# +# +# Input: n = 3 +# Output: false +# +# +# +# Constraints: +# +# +# -2^31 <= n <= 2^31 - 1 +# +# +# +# Follow up: Could you solve it without loops/recursion? +# + +# @lc code=start +class Solution: + def isPowerOfTwo(self, n: int) -> bool: + if (n==1): + return True + + n_bin = bin(n) + n_bin = n_bin[2:] + + if (n_bin[0]!= '1'): + return False + + elif (n_bin.count('1')>1): + return False + + return True + +# @lc code=end +