20240802 Finished
This commit is contained in:
parent
bc583588af
commit
bc296e9e95
|
@ -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;
|
||||
mod p69_sqrtx;
|
||||
mod p50_powx_n;
|
41
src/problem/p50_powx_n.rs
Normal file
41
src/problem/p50_powx_n.rs
Normal file
|
@ -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() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user