Skip to content

Commit

Permalink
Accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo-Shakib committed Oct 30, 2024
1 parent 7927198 commit b21c4ec
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions easy/231-power-of-two.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b21c4ec

Please sign in to comment.