-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
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 | ||
|