leetcode

Log | Files | Refs | README

commit ac0e24ff74150cfb7543d1d0b6910f6464783679
parent 430b143c71fb36f6120fb32bc2b79618d0be2da2
Author: ling0x <ling0x@users.noreply.github.com>
Date:   Wed, 22 Apr 2026 00:08:21 +0100

exercise

Diffstat:
Msrc/array/longest_common_prefix.rs | 44++++++++++++++++++++++++++++++++++++++++++--
Msrc/hash_table/two_sum.rs | 2+-
Msrc/main.rs | 4++--
3 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/src/array/longest_common_prefix.rs b/src/array/longest_common_prefix.rs @@ -1,4 +1,6 @@ -pub fn longest_common_prefix(strs: Vec<String>) -> String { +use tracing::info; + +pub fn longest_common_prefix_1(strs: Vec<String>) -> String { let mut seen = std::collections::HashSet::<char>::new(); let mut seen_seen = std::collections::HashSet::<char>::new(); let mut common = Vec::<char>::new(); @@ -21,14 +23,52 @@ pub fn longest_common_prefix(strs: Vec<String>) -> String { } } + info!("{common_common:?}"); + String::from_iter(common_common) } #[cfg(test)] mod tests { + use tracing::info; + + use crate::{ + array::longest_common_prefix::longest_common_prefix_1, + test_utils::benchmark::init_benchmark_tracing, + }; + + struct Case { + input: Vec<String>, + output: String, + } + + fn full_cases() -> Vec<Case> { + vec![ + Case { + input: vec![ + "flower".to_string(), + "flow".to_string(), + "flight".to_string(), + ], + output: "fl".to_string(), + }, + Case { + input: vec!["dog".to_string(), "racecar".to_string(), "car".to_string()], + output: String::new(), + }, + ] + } + + fn run_solver_cases(solver_name: &str, solver: fn(Vec<String>) -> String, cases: Vec<Case>) { + init_benchmark_tracing(); + info!("{solver_name}"); + for case in cases { + assert_eq!(solver(case.input), case.output); + } + } #[test] fn longest_common_prefix_1_cases() { - assert!(true) + run_solver_cases("solver 1", longest_common_prefix_1, full_cases()); } } diff --git a/src/hash_table/two_sum.rs b/src/hash_table/two_sum.rs @@ -294,7 +294,7 @@ mod tests { } #[test] - // #[ignore] + #[ignore] fn two_sum_9_cases() { run_solver_cases("two_sum_9", two_sum_9, full_cases()); } diff --git a/src/main.rs b/src/main.rs @@ -1,5 +1,5 @@ use leetcode::{ - array::longest_common_prefix::longest_common_prefix, hash_table::two_sum::two_sum_9, + array::longest_common_prefix::longest_common_prefix_1, hash_table::two_sum::two_sum_9, }; use tracing::info; use tracing_subscriber::fmt; @@ -7,7 +7,7 @@ use tracing_subscriber::fmt; fn main() { fmt().with_target(false).compact().init(); info!("-----------------------"); - longest_common_prefix(vec![ + longest_common_prefix_1(vec![ "flower".to_string(), "flow".to_string(), "flight".to_string(),