Skip to content

Commit

Permalink
leetcode: Add 0599
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 13, 2024
1 parent 7c3a510 commit df14ee3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0599.minimum-index-sum-of-two-lists/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0599-minimum-index-sum-of-two-lists"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0599.minimum-index-sum-of-two-lists/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
91 changes: 91 additions & 0 deletions src/leetcode/0599.minimum-index-sum-of-two-lists/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.

use std::cmp::Ordering;
use std::collections::HashMap;

// HashTable
pub fn find_restaurant1(list1: Vec<String>, list2: Vec<String>) -> Vec<String> {
debug_assert!(!list1.is_empty());
debug_assert!(!list2.is_empty());

// 用于存储 list1 中的 (单词, 索引)
let map1: HashMap<String, usize> = list1
.into_iter()
.enumerate()
.map(|(index, s)| (s, index))
.collect();

let mut least_index_sum: usize = usize::MAX;
let mut common_strings = Vec::new();

// 然后遍历 list2
for (index2, s2) in list2.into_iter().enumerate() {
if let Some(index1) = map1.get(&s2) {
let sum = index1 + index2;
match sum.cmp(&least_index_sum) {
Ordering::Less => {
common_strings.clear();
common_strings.push(s2);
least_index_sum = sum;
}
Ordering::Equal => common_strings.push(s2),
Ordering::Greater => (),
}
}
}

common_strings
}

pub type SolutionFn = fn(Vec<String>, Vec<String>) -> Vec<String>;

fn check_solution(func: SolutionFn) {
fn to_string_vec(strs: &[&str]) -> Vec<String> {
strs.iter().map(|s| s.to_string()).collect()
}

let list1 = &["Shogun", "Tapioca Express", "Burger King", "KFC"];
let list2 = &[
"Piatti",
"The Grill at Torrey Pines",
"Hungry Hunter Steakhouse",
"Shogun",
];
let exp = &["Shogun"];
assert_eq!(
func(to_string_vec(list1), to_string_vec(list2)),
to_string_vec(exp)
);

let list1 = &["Shogun", "Tapioca Express", "Burger King", "KFC"];
let list2 = &["KFC", "Shogun", "Burger King"];
let exp = &["Shogun"];
assert_eq!(
func(to_string_vec(list1), to_string_vec(list2)),
to_string_vec(exp)
);

let list1 = &["happy", "sad", "good"];
let list2 = &["sad", "happy", "good"];
let exp = &["sad", "happy"];
assert_eq!(
func(to_string_vec(list1), to_string_vec(list2)),
to_string_vec(exp)
);
}

fn main() {
check_solution(find_restaurant1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, find_restaurant1};

#[test]
fn test_find_restaurant1() {
check_solution(find_restaurant1);
}
}

0 comments on commit df14ee3

Please sign in to comment.