Question 50, Pow(x, n)

Address: leetcode-cn.com/problems/po…

N >0 and n<0 can be discussed

var myPow = function(x, n) {
 return n === 0 ? 1:n < 0 ? 1/myPow(x,-n) : ( n % 2 === 0 ? myPow(x*x,n/2) : x*myPow(x,n-1));
};
Copy the code
Execution time: 80 ms, beating 90.76% of users in all JavaScript commits
Memory consumption: 38.9 MB, beating 26.44% of all JavaScript commits