File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 231. Power of Two
2+
3+ Given an integer n, write a function to determine if it is a power of two.
4+
5+
6+
7+ Example 1:
8+
9+ Input: n = 1
10+ Output: true
11+ Explanation: 20 = 1
12+ Example 2:
13+
14+ Input: n = 16
15+ Output: true
16+ Explanation: 24 = 16
17+ Example 3:
18+
19+ Input: n = 3
20+ Output: false
21+ Example 4:
22+
23+ Input: n = 4
24+ Output: true
25+ Example 5:
26+
27+ Input: n = 5
28+ Output: false
29+
30+
31+ Constraints:
32+
33+ -231 <= n <= 231 - 1
34+
35+ ``` python
36+ def isPowerOfTwo (self , n : int ) -> bool :
37+ # return n > 0 and n == n & -n
38+ if not n or n < 1 : return False
39+ return n == n & (- n)
40+ ```
41+
42+ ```
43+ Runtime: 24 ms, faster than 95.06% of Python3 online submissions for Power of Two.
44+ Memory Usage: 14.1 MB, less than 99.94% of Python3 online submissions for Power of Two.
45+ ```
You can’t perform that action at this time.
0 commit comments