https://leetcode.com/problems/powx-n/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function(x, n) {
n = Math.floor(n)
if (n === 0) {
return 1
}
if (n < 0) {
n = -n
x = 1 / x
}
return n % 2 === 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2)
};