20240801 Finished
This commit is contained in:
parent
dc662fe156
commit
bc583588af
|
@ -191,3 +191,4 @@ mod p137_single_number_ii;
|
|||
mod p201_bitwise_and_of_numbers_range;
|
||||
mod p66_plus_one;
|
||||
mod p172_factorial_trailing_zeroes;
|
||||
mod p69_sqrtx;
|
46
src/problem/p69_sqrtx.rs
Normal file
46
src/problem/p69_sqrtx.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
|
||||
/**
|
||||
* [69] Sqrt(x)
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn my_sqrt(x: i32) -> i32 {
|
||||
// 牛顿迭代法
|
||||
if x == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let c = x as f64;
|
||||
let mut x = x as f64;
|
||||
|
||||
loop {
|
||||
let xi = 0.5 * (x + c / x);
|
||||
if (x - xi).abs() < 1e-7 {
|
||||
break;
|
||||
}
|
||||
x = xi;
|
||||
}
|
||||
|
||||
return x as i32;
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_69() {
|
||||
assert_eq!(0, Solution::my_sqrt(0));
|
||||
assert_eq!(1, Solution::my_sqrt(1));
|
||||
assert_eq!(1, Solution::my_sqrt(2));
|
||||
assert_eq!(2, Solution::my_sqrt(4));
|
||||
assert_eq!(2, Solution::my_sqrt(8));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user