From bc296e9e95bb4c2a7c7a3582b2a5f300eb0010ab Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 2 Aug 2024 11:37:58 +0800 Subject: [PATCH] 20240802 Finished --- src/problem/mod.rs | 3 ++- src/problem/p50_powx_n.rs | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/problem/p50_powx_n.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index bbc0641..1fa1cac 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -191,4 +191,5 @@ mod p137_single_number_ii; mod p201_bitwise_and_of_numbers_range; mod p66_plus_one; mod p172_factorial_trailing_zeroes; -mod p69_sqrtx; \ No newline at end of file +mod p69_sqrtx; +mod p50_powx_n; \ No newline at end of file diff --git a/src/problem/p50_powx_n.rs b/src/problem/p50_powx_n.rs new file mode 100644 index 0000000..9140dd0 --- /dev/null +++ b/src/problem/p50_powx_n.rs @@ -0,0 +1,41 @@ +/** + * [50] Pow(x, n) + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn my_pow(x: f64, n: i32) -> f64 { + if n < 0 { + 1f64 / Self::quick_pow(x, -n) + } else { + Self::quick_pow(x, n) + } + } + + fn quick_pow(x: f64, n: i32) -> f64 { + if n == 0 { + 1f64 + } else { + let y = Self::quick_pow(x, n / 2); + if n % 2 == 0 { + y * y + } else { + y * y * x + } + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_50() { + } +}