- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathpowx-n.py
More file actions
Latest commit
21 lines (18 loc) · 534 Bytes
/
Copy pathpowx-n.py
File metadata and controls
21 lines (18 loc) · 534 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#https://leetcode.com/problems/powx-n/
classSolution:
defmyPow(self, x: float, n: int) ->float:
# Base case
ifn==0:
return1
# determine whether to divide by the answer
ifn<0:
div=True
else:
div=False
n=abs(n)
ans=1
whilen>0:
ans*=x
print(ans)
n-=1# decrement n by 1
return1/ansifdivelseans# divide by the answer if n is negative