Skip to content

Commit 4b9903f

Browse files
committed
二刷50
1 parent d1875e1 commit 4b9903f

File tree

6 files changed

+70
-24
lines changed

6 files changed

+70
-24
lines changed

docs/0050-powx-n.adoc

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33

44
{leetcode}/problems/powx-n/[LeetCode - Pow(x, n)^]
55

6-
首先,可以把"一半的计算结果"存储起来,节省一半的递归调用;
7-
8-
其次,没想到还需要处理"无穷"的情况!
9-
10-
另外,思考一下,如果使用迭代来实现?
11-
126
Implement http://www.cplusplus.com/reference/valarray/pow/[pow(_x_, _n_)^], which calculates _x_ raised to the power _n_ (x^n^).
137

148
*Example 1:*
@@ -42,10 +36,45 @@ Implement http://www.cplusplus.com/reference/valarray/pow/[pow(_x_, _n_)^], whic
4236
* -100.0 < _x_ < 100.0
4337
* _n_ is a 32-bit signed integer, within the range [-2^31^, 2^31 ^- 1]
4438
39+
== 思路分析
40+
41+
image::images/0050-01.png[{image_attr}]
42+
43+
image::images/0050-02.png[{image_attr}]
44+
45+
首先,可以把"一半的计算结果"存储起来,节省一半的递归调用;
46+
47+
其次,没想到还需要处理"无穷"的情况!
48+
49+
另外,思考一下,如果使用迭代来实现?
50+
51+
快速幂的算法能看懂,但代码不知道怎么写。
4552

4653
[[src-0050]]
54+
[tabs]
55+
====
56+
一刷::
57+
+
58+
--
4759
[{java_src_attr}]
4860
----
4961
include::{sourcedir}/_0050_PowXN.java[tag=answer]
5062
----
63+
--
64+
65+
二刷::
66+
+
67+
--
68+
[{java_src_attr}]
69+
----
70+
include::{sourcedir}/_0050_PowXN_2.java[tag=answer]
71+
----
72+
--
73+
====
74+
75+
== 参考资料
76+
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) - 快速幂,清晰图解^]
79+
5180

docs/images/0050-01.png

17.2 KB
Loading

docs/images/0050-02.png

24.6 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
|{doc_base_url}/0113-path-sum-ii.adoc[题解]
581581
|回溯
582582

583+
|{counter:codes}
584+
|{leetcode_base_url}/powx-n/[50. Pow(x, n)^]
585+
|{doc_base_url}/0050-powx-n.adoc[题解]
586+
|分治,快速幂,迭代实现还需要再思考理解一下。
587+
583588
|===
584589

585590
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0050_PowXN.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class _0050_PowXN {
4444
* Runtime: 1 ms, faster than 94.10% of Java online submissions for Pow(x, n).
4545
*
4646
* Memory Usage: 34.4 MB, less than 5.88% of Java online submissions for Pow(x, n).
47+
*
48+
* @author D瓜哥 · https://www.diguage.com
49+
* @since 2020-01-13 21:19
4750
*/
4851
public double myPow(double x, int n) {
4952
if (n == 0) {
@@ -61,24 +64,6 @@ public double myPow(double x, int n) {
6164

6265
return (n % 2 == 0 ? 1.0 : x) * semiResult * semiResult;
6366
}
64-
65-
/**
66-
* Time Limit Exceeded
67-
* <p>
68-
* Copy from:
69-
*/
70-
public double myPowTimeout(double x, int n) {
71-
if (n == 0) {
72-
return 1;
73-
}
74-
if (n < 0) {
75-
x = 1 / x;
76-
n = -n;
77-
}
78-
return n % 2 == 0 ? myPowTimeout(x, n / 2) * myPowTimeout(x, n / 2)
79-
: x * myPowTimeout(x, n / 2) * myPowTimeout(x, n / 2);
80-
}
81-
8267
// end::answer[]
8368

8469

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0050_PowXN_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2020-01-13 21:19
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+
if (n == 1) {
17+
return x;
18+
}
19+
if (n < 0) {
20+
x = 1 / x;
21+
n = -n;
22+
}
23+
double tmp = myPow(x, n / 2);
24+
return tmp * tmp * ((n & 1) == 0 ? 1 : x);
25+
}
26+
// end::answer[]
27+
}

0 commit comments

Comments
 (0)