算法笔记: 力扣#231 2的幂

问题描述


解法


分析

Python 实现

1
2
3
4
5
6
7
class (object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
return (n > 0) and ((n & n-1) == 0)

Java 实现

1
2
3
4
5
class {
public boolean isPowerOfTwo(int n) {
return (n > 0) && ((n & n-1) == 0);
}
}

时间复杂度

O(1).

空间复杂度

O(1).

链接


231. Power of Two
231. 2的幂
(English version) Algorithm Notes: Leetcode#231 Power of Two