We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 7c1a5ff + 5cc6a24 commit 60fba98Copy full SHA for 60fba98
rust/0050-powx-n.rs
@@ -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
22
23
+ 1.0 / res
24
25
26
+}
0 commit comments