Skip to content

Commit 7043bd7

Browse files
solves is power of 2 in python
1 parent 6467c64 commit 7043bd7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
![problems-solved-python](https://img.shields.io/badge/Python-32/1571-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77

8+
🔒 = Subscription Content
9+
810
## Problems
911
| #Number | Name | Difficulty | Solution | Youtube |
1012
|:--------:|------|:----------:|:--------:|:----------------------:|
@@ -49,7 +51,7 @@
4951
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/TwoSumIIInputArrayIsSorted.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/two_sum_ii.py) |
5052
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnTitle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/excel_sheet_column_title.py) |
5153
| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MajorityElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/majority_element.py) |
52-
| 170 | [🔒 Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | Easy | |
54+
| 170 | 🔒 [Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | Easy | |
5355
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnNumber.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/excel_sheet_column_number.py) |
5456
| 172 | [Factoring Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FactorialTrailingZeros.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/factorial_trailing_zeroes.py) |
5557
| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RotateArray.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/rotate_array.py) |
@@ -65,7 +67,7 @@
6567
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ContainsDuplicateII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/contains_duplicate_ii.py) |
6668
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyStack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_stack_using_queues.py) |
6769
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/InvertBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/invert_binary_tree.py) |
68-
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) |
70+
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_power_of_2.py) |
6971
| 232 | [Implement Queue Using Stacks]() | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) |
7072
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) |
7173
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LowestCommonAncestorOfBinarySearchTree.java) |

python/is_power_of_2.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def isPowerOfTwo(self, n: int) -> bool:
3+
if n == 1:
4+
return True
5+
if n == 0:
6+
return False
7+
if n % 2 == 1:
8+
return False
9+
return self.isPowerOfTwo(n // 2)
10+
11+
# alternate version
12+
# def isPowerOfTwo(self, n: int) -> bool:
13+
# return n > 0 and (n & (n - 1)) == 0

0 commit comments

Comments
 (0)