Leetcode 50 Pow(x, n) javascript

实现 pow(x, n) ,即计算 x 的 n 次幂函数。

/**
 * @param {number} x
 * @param {number} n
 * @return {number}
 */
var myPow = function(x, n) {
	return x ** n; 
};
//Runtime: 76 ms
//递归思路
var myPow = function(x, n) {
    if(!n) return 1;
    if(n < 0) return 1 / myPow(x, -n);
    if(n % 2){
        return x * myPow(x, n-1);
    }
    return myPow(x*x, n/2);
};
//Runtime: 72 ms, faster than 100.00% of JavaScript online submissions for Pow(x, n).
//非递归,用位运算,最优解
var myPow = function(x, n) {
    if (n < 0) {
        n = -n;
        x = 1 / x;
    }
    let res = 1;
    while (n > 0) {
        if (n % 2 === 1) {
            res *= x;
            --n;
        }
        x *= x;
        n /= 2;
    }
    return res;
};
//Runtime: 72 ms

猜你喜欢

转载自blog.csdn.net/Csoap2/article/details/86716720