Skip to content

Commit e3313df

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 231
1 parent 69f936c commit e3313df

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
- [227 Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/description/)
209209
- [228 Summary Ranges](https://leetcode.com/problems/summary-ranges/description/)
210210
- [230 Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
211+
- [231 Power of Two](https://leetcode.com/problems/power-of-two/description/)
211212
- [236 Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
212213
- [238 Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/)
213214
- [242 Valid Anagram](https://leetcode.com/problems/valid-anagram/description/)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def isPowerOfTwo(self, n: int) -> bool:
5+
"""
6+
Given an integer n, return true if it is a power of two.
7+
Otherwise, return false.
8+
9+
An integer n is a power of two, if there exists an integer x such that n == 2x.
10+
"""
11+
num_ones = 0
12+
while n > 0:
13+
num_ones += n & 1 # Get the last bit
14+
n >>= 1 # shift right
15+
return num_ones == 1

tests/test_231_power_of_two.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._231_power_of_two import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(1, True),
10+
(16, True),
11+
(3, False),
12+
],
13+
)
14+
def test_func(n: int, expected: bool):
15+
"""Tests the solution of a LeetCode problem."""
16+
is_power_of_two = Solution().isPowerOfTwo(n)
17+
assert is_power_of_two is expected

0 commit comments

Comments
 (0)