Skip to content

Commit

Permalink
leetcode: Add 0050
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 6, 2024
1 parent e628c33 commit 59fc307
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0050.powx-n/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0050-powx-n"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0050.powx-n/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
83 changes: 83 additions & 0 deletions src/leetcode/0050.powx-n/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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);
}
}
37 changes: 26 additions & 11 deletions src/leetcode/0367.valid-perfect-square/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 59fc307

Please sign in to comment.