add initial data

This commit is contained in:
timeshifter committed 2026-09-04 15:56:45 +02:00
1 parent 048a82b101
commit 51d935d06b
66 files changed
+1329

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "leetcode"
version = "0.1.0"
@@ -0,0 +1,6 @@
[package]
name = "leetcode"
version = "0.1.0"
edition = "2024"
[dependencies]
@@ -0,0 +1,66 @@
pub fn letter_combinations(digits: String) -> Vec<String> {
let mut current = Vec::new();
current.push("".to_string());
let mut next = Vec::new();
for digit in digits.chars() {
for entry in current.drain(..) {
for letter in to_letter(digit) {
let mut this_entry = entry.clone();
this_entry.push(letter);
next.push(this_entry);
}
}
std::mem::swap(&mut current, &mut next);
next.clear();
}
current
}
fn to_letter(digit: char) -> impl Iterator<Item = char> {
let letters: &'static [char] = match digit {
'2' => &['a', 'b', 'c'],
'3' => &['d', 'e', 'f'],
'4' => &['g', 'h', 'i'],
'5' => &['j', 'k', 'l'],
'6' => &['m', 'n', 'o'],
'7' => &['p', 'q', 'r', 's'],
'8' => &['t', 'u', 'v'],
'9' => &['w', 'x', 'y', 'z'],
_ => panic!(),
};
letters.iter().copied()
}
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::letter_combinations;
#[test]
fn test_1() {
let letters = letter_combinations("23".to_string())
.into_iter()
.collect::<HashSet<_>>();
for example in ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] {
assert!(letters.contains(example));
}
}
#[test]
fn test_2() {
let letters = letter_combinations("2".to_string())
.into_iter()
.collect::<HashSet<_>>();
for example in ["a", "b", "c"] {
assert!(letters.contains(example));
}
}
}