{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "096ba255d3f74daca6719e44801429dd", "keywords": "递归,数学", "title": "Pow(x, n)", "desc": [ { "content": "\n

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

 

示例 1:

输入:x = 2.00000, n = 10
输出:
1024.00000

示例 2:

输入:x = 2.10000, n = 3
输出:
9.26100

示例 3:

输入:x = 2.00000, n = -2
输出:
0.25000
解释:
2-2 = 1/22 = 1/4 = 0.25

 

提示:

", "language": "markdown" } ], "answer": [ { "content": "", "language": "java" } ], "prepared": [ [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ] ], "template": { "content": "public class Solution {\n\tpublic double myPow(double x, int n) {\n\t\tif (n < 0) {\n\n\t\t\treturn 1 / pow(x, -n);\n\t\t} else {\n\n\t\t\treturn pow(x, n);\n\t\t}\n\t}\n\n\tprivate double pow(double x, int n) {\n\n\t\tif (n == 0) {\n\t\t\treturn 1.0;\n\t\t}\n\t\tif (n == 1) {\n\t\t\treturn x;\n\t\t}\n\t\tdouble val = pow(x, n / 2);\n\n\t\tif (n % 2 == 0) {\n\t\t\treturn val * val;\n\t\t} else {\n\t\t\treturn val * val * x;\n\t\t}\n\t}\n}\n", "language": "java" }, "node_id": "dailycode-aa14a10d3dbc4e2a983c7cffd4eca240", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600471013" }