Skip to content

Commit 5cc6a24

Browse files
committedJan 23, 2024
Pow(x, n) solution
1 parent cc4b0a5 commit 5cc6a24

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎rust/0050-powx-n.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn my_pow(x: f64, n: i32) -> f64 {
3+
fn helper(x: f64, n: i32) -> f64 {
4+
match (x, n) {
5+
(0.0, _) => 0.0,
6+
(_, 0) => 1.0,
7+
_ => {
8+
let res = helper(x * x, n / 2);
9+
if n % 2 == 0 {
10+
res
11+
} else {
12+
x * res
13+
}
14+
}
15+
}
16+
}
17+
18+
let res = helper(x, n.abs());
19+
20+
if n >= 0 {
21+
res
22+
} else {
23+
1.0 / res
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.