From 59fc30722962f2d7591f338706f46b1289d452e2 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Mon, 6 May 2024 17:53:22 +0800 Subject: [PATCH] leetcode: Add 0050 --- src/leetcode/0050.powx-n/Cargo.toml | 7 ++ src/leetcode/0050.powx-n/index.md | 4 + src/leetcode/0050.powx-n/src/main.rs | 83 +++++++++++++++++++ .../0367.valid-perfect-square/src/main.rs | 37 ++++++--- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 src/leetcode/0050.powx-n/Cargo.toml create mode 100644 src/leetcode/0050.powx-n/index.md create mode 100644 src/leetcode/0050.powx-n/src/main.rs diff --git a/src/leetcode/0050.powx-n/Cargo.toml b/src/leetcode/0050.powx-n/Cargo.toml new file mode 100644 index 00000000..37e34d83 --- /dev/null +++ b/src/leetcode/0050.powx-n/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0050-powx-n" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0050.powx-n/index.md b/src/leetcode/0050.powx-n/index.md new file mode 100644 index 00000000..77a618ba --- /dev/null +++ b/src/leetcode/0050.powx-n/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0050.powx-n/src/main.rs b/src/leetcode/0050.powx-n/src/main.rs new file mode 100644 index 00000000..b6013fbd --- /dev/null +++ b/src/leetcode/0050.powx-n/src/main.rs @@ -0,0 +1,83 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +pub fn my_pow1(x: f64, n: i32) -> f64 { + assert!(x != 0.0 || n > 0); + if n == 0 { + return 1.0; + } + + let mut n: i64 = n as i64; + let mut x = x; + if n < 0 { + n = -n; + x = 1.0 / x; + } + + let mut ans = 1.0; + + // 分治 + while n > 0 { + if n % 2 == 0 { + n /= 2; + x *= x; + } else { + n -= 1; + ans *= x; + } + } + + ans +} + +pub type SolutionFn = fn(f64, i32) -> f64; + +fn check_solution(func: SolutionFn) { + fn nearly_equal(a: f64, b: f64) { + println!("a: {a}, b: {b}"); + + let diff: f64 = (a - b).abs(); + let scale: f64 = 100_000.0; + + assert!(diff * scale < 1.0); + } + + let x = 2.00000; + let n = 10; + nearly_equal(func(x, n), 1024.00000); + + let x = 2.10000; + let n = 3; + nearly_equal(func(x, n), 9.26100); + + let x = 2.00000; + let n = -2; + nearly_equal(func(x, n), 0.25000); + + let x = 0.00001; + let n = i32::MAX; + nearly_equal(func(x, n), 0.00000); + + let x = 2.00000; + let n = i32::MIN; + nearly_equal(func(x, n), 0.00000); + + let x = -1.00000; + let n = i32::MIN; + nearly_equal(func(x, n), 1.00000); +} + +fn main() { + check_solution(my_pow1); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, my_pow1}; + + #[test] + fn test_my_pow1() { + check_solution(my_pow1); + } +} diff --git a/src/leetcode/0367.valid-perfect-square/src/main.rs b/src/leetcode/0367.valid-perfect-square/src/main.rs index 3bb3fc15..243d26e8 100644 --- a/src/leetcode/0367.valid-perfect-square/src/main.rs +++ b/src/leetcode/0367.valid-perfect-square/src/main.rs @@ -2,12 +2,16 @@ // Use of this source is governed by General Public License that can be found // in the LICENSE file. -pub fn is_perfect_square(num: i32) -> bool { - use std::cmp::Ordering; +use std::cmp::Ordering; + +// Binary Search +pub fn is_perfect_square1(num: i32) -> bool { + assert!(num >= 1); let mut left = 1; let mut right = num; let num64 = num as i64; + while left <= right { let middle = left + (right - left) / 2; let square = middle as i64 * middle as i64; @@ -20,16 +24,27 @@ pub fn is_perfect_square(num: i32) -> bool { false } -fn check_solution() { - assert!(is_perfect_square(1)); - assert!(!is_perfect_square(8)); - assert!(is_perfect_square(9)); - assert!(!is_perfect_square(10)); - assert!(!is_perfect_square(14)); - assert!(is_perfect_square(16)); - assert!(!is_perfect_square(i32::MAX)); +pub type SolutionFn = fn(i32) -> bool; + +fn check_solution(func: SolutionFn) { + assert!(func(1)); + assert!(!func(8)); + assert!(func(9)); + assert!(!func(10)); + assert!(func(16)); + assert!(!func(i32::MAX)); } fn main() { - check_solution(); + check_solution(is_perfect_square1); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, is_perfect_square1}; + + #[test] + fn test_is_perfect_square1() { + check_solution(is_perfect_square1); + } }