Skip to content

Commit aa473df

Browse files
committed
三刷50
1 parent 964f811 commit aa473df

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

Diff for: docs/0050-powx-n.adoc

+35-27
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
[#0050-powx-n]
22
= 50. Pow(x, n)
33

4-
{leetcode}/problems/powx-n/[LeetCode - Pow(x, n)^]
4+
https://leetcode.cn/problems/powx-n/[LeetCode - 50. Pow(x, n) ^]
55

6-
Implement http://www.cplusplus.com/reference/valarray/pow/[pow(_x_, _n_)^], which calculates _x_ raised to the power _n_ (x^n^).
6+
实现 https://www.cplusplus.com/reference/valarray/pow/[pow(x, n)] ,即计算 `x` 的整数 `n` 次幂函数(即,`x^n^`)。
77

8-
*Example 1:*
8+
*示例 1:*
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:* 2.00000, 10
13-
*Output:* 1024.00000
14-
----
10+
....
11+
输入:x = 2.00000, n = 10
12+
输出:1024.00000
13+
....
1514

16-
*Example 2:*
15+
*示例 2:*
1716

18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* 2.10000, 3
21-
*Output:* 9.26100
22-
----
17+
....
18+
输入:x = 2.10000, n = 3
19+
输出:9.26100
20+
....
2321

24-
*Example 3:*
25-
26-
[subs="verbatim,quotes,macros"]
27-
----
28-
*Input:* 2.00000, -2
29-
*Output:* 0.25000
30-
*Explanation:* 2^-2^ = 1/2^2^ = 1/4 = 0.25
31-
----
22+
*示例 3:*
3223

33-
*Note:*
24+
....
25+
输入:x = 2.00000, n = -2
26+
输出:0.25000
27+
解释:2-2 = 1/22 = 1/4 = 0.25
28+
....
3429

30+
*提示:*
3531

36-
* -100.0 < _x_ < 100.0
37-
* _n_ is a 32-bit signed integer, within the range [-2^31^, 2^31 ^- 1]
32+
* `-100.0 < x < 100.0`
33+
* `-2^31^ \<= n \<= 2^31^-1`
34+
* `n` 是一个整数
35+
* 要么 `x` 不为零,要么 `n > 0`
36+
* `-10^4^ \<= x^n^ \<= 10^4^`
3837
3938
== 思路分析
4039

@@ -70,11 +69,20 @@ include::{sourcedir}/_0050_PowXN.java[tag=answer]
7069
include::{sourcedir}/_0050_PowXN_2.java[tag=answer]
7170
----
7271
--
72+
73+
三刷::
74+
+
75+
--
76+
[{java_src_attr}]
77+
----
78+
include::{sourcedir}/_0050_PowXN_3.java[tag=answer]
79+
----
80+
--
7381
====
7482

7583
== 参考资料
7684

77-
. https://leetcode.cn/problems/powx-n/solutions/238559/powx-n-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[50. Pow(x, n) - 官方题解^]
78-
. https://leetcode.cn/problems/powx-n/solutions/241471/50-powx-n-kuai-su-mi-qing-xi-tu-jie-by-jyd/?envType=study-plan-v2&envId=selected-coding-interview[50. Pow(x, n) - 快速幂,清晰图解^]
85+
. https://leetcode.cn/problems/powx-n/solutions/238559/powx-n-by-leetcode-solution/[50. Pow(x, n) - 官方题解^]
86+
. https://leetcode.cn/problems/powx-n/solutions/241471/50-powx-n-kuai-su-mi-qing-xi-tu-jie-by-jyd/[50. Pow(x, n) - 快速幂,清晰图解^]
7987

8088

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ endif::[]
185185
|{doc_base_url}/0088-merge-sorted-array.adoc[题解]
186186
|✅ `num1` 后面空着,则从后向前合并。
187187

188+
|{counter:codes2503}
189+
|{leetcode_base_url}/powx-n/[50. Pow(x, n)^]
190+
|{doc_base_url}/0050-powx-n.adoc[题解]
191+
|✅ 递归很简单,抽空再思考一下非递归形式。
192+
188193
|===
189194

190195
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0050_PowXN_3 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-07 14:26:30
8+
*/
9+
public double myPow(double x, long n) {
10+
if (x == 0) {
11+
return 0;
12+
}
13+
if (n == 0) {
14+
return 1;
15+
}
16+
boolean negative = n < 0;
17+
n = Math.abs(n);
18+
double result = 1;
19+
double bin = myPow(x, n / 2);
20+
if (n % 2 == 1) {
21+
result = x * bin * bin;
22+
} else {
23+
result = bin * bin;
24+
}
25+
return negative ? 1 / result : result;
26+
}
27+
// end::answer[]
28+
}

0 commit comments

Comments
 (0)