We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
出处:LeetCode 算法第50题 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 输出: 9.26100 示例 3: 输入: 2.00000, -2 输出: 0.25000 解释: 2-2 = 1/22 = 1/4 = 0.25 说明: -100.0 < x < 100.0 n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。
出处:LeetCode 算法第50题
实现 pow(x, n) ,即计算 x 的 n 次幂函数。
示例 1:
输入: 2.00000, 10 输出: 1024.00000
示例 2:
输入: 2.10000, 3 输出: 9.26100
示例 3:
输入: 2.00000, -2 输出: 0.25000 解释: 2-2 = 1/22 = 1/4 = 0.25
说明:
1.直接使用Math.pow
2.采用递归的方式
/** * @param {number} x * @param {number} n * @return {number} */ var myPow = function (x, n) { if (n == 0) return 1; if (n == 1) return x; if (n == -1) return 1 / x; if (n % 2 == 0) { var temp = myPow(x, n / 2); return temp * temp; } else { return x * myPow(x, n - 1); } }; console.log(myPow(2.1,3));
The text was updated successfully, but these errors were encountered:
No branches or pull requests
习题
思路
1.直接使用Math.pow
2.采用递归的方式
解答
The text was updated successfully, but these errors were encountered: