Skip to content

Commit 60fba98

Browse files
authored
Merge pull request #3249 from nkawaller/pow-x-n
Create: 0050-powx-n.rs
2 parents 7c1a5ff + 5cc6a24 commit 60fba98

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: 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)