-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path231PowerOfTwo.py
More file actions
56 lines (52 loc) · 1 KB
/
231PowerOfTwo.py
File metadata and controls
56 lines (52 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
"""
"""
Comments two solving way
"""
"""
My
"""
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
return n>0 and 2147483648 % n == 0
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n <=0:
return False
if n!=1 and n%2 !=0:
return False
if n == 1 or n==2:
return True
x = n
while x!= 1:
if (x/2) %2 != 0:
return False
x /= 2
x = int(x)
if x == 2:
break
return True
"""
Fast
"""
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
num = n
while (num > 1):
if num % 2 == 1:
return False
num /= 2
if num == 1:
return True
return False