forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpow.py
42 lines (36 loc) · 777 Bytes
/
pow.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
#!/usr/bin/env python
'''
Leetcode: pow(x,n)
pow(x,n) = x^n
'''
from __future__ import division
import random
# Divide and Conquer
# remember that n can be < 0
# O(logn)
def pow(x, n):
if n == 0: return 1
elif n == 1: return x
elif n > 1:
half = pow(x, n//2)
if n % 2 == 0: return half*half
else: return half*half*x
else: # n < 0
return 1/pow(x, -n)
def pow2(x,n):
if n == 0: return 1
if n == 1: return x
m = -n if n < 0 else n
ret = 1
while m > 0:
if m & 1: ret *= x
# x^n --> (x^2)^(n/2)
x *= x
m >>= 1
return 1/ret if n < 0 else ret
if __name__ == '__main__':
print pow2(10,2)
print pow2(10,0)
print pow2(5,3)
print pow2(5,-3)
print pow2(2,10)