-
Notifications
You must be signed in to change notification settings - Fork 26
/
326.PowerofThree.py
44 lines (35 loc) · 973 Bytes
/
326.PowerofThree.py
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
'''
Given an integer n, return true if it is a power of
three. Otherwise, return false.
An integer n is a power of three, if there exists an
integer x such that n == 3x.
Example:
Input: n = 27
Output: true
Example:
Input: n = 0
Output: false
Example:
Input: n = 9
Output: true
Example:
Input: n = 45
Output: false
Constraints:
- -2^31 <= n <= 2^31 - 1
Follow up: Could you solve it without loops/recursion?
'''
#Difficulty: Easy
#21038 / 21038 test cases passed.
#Runtime: 188 ms
#Memory Usage: 14.3 MB
#Runtime: 188 ms, faster than 6.05% of Python3 online submissions for Power of Three.
#Memory Usage: 14.3 MB, less than 47.05% of Python3 online submissions for Power of Three.
class Solution:
def isPowerOfThree(self, n: int) -> bool:
x = 0
while 3 ** x <= n:
if 3 ** x == n:
return True
x += 1
return False