File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 208
208
- [ 227 Basic Calculator II] ( https://leetcode.com/problems/basic-calculator-ii/description/ )
209
209
- [ 228 Summary Ranges] ( https://leetcode.com/problems/summary-ranges/description/ )
210
210
- [ 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/ )
211
212
- [ 236 Lowest Common Ancestor of a Binary Tree] ( https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ )
212
213
- [ 238 Product of Array Except Self] ( https://leetcode.com/problems/product-of-array-except-self/description/ )
213
214
- [ 242 Valid Anagram] ( https://leetcode.com/problems/valid-anagram/description/ )
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments