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"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "leetcode"
version = "0.1.0"
edition = "2024"
[dependencies]
+59
View File
@@ -0,0 +1,59 @@
static ROMAN_VALUES: [(i32, &str); 13] = [
(1, "I"),
(4, "IV"),
(5, "V"),
(9, "IX"),
(10, "X"),
(40, "XL"),
(50, "L"),
(90, "XC"),
(100, "C"),
(400, "CD"),
(500, "D"),
(900, "CM"),
(1000, "M"),
];
pub fn roman_to_int(s: String) -> i32 {
let mut s = s;
let mut result = 0;
for (num, letters) in ROMAN_VALUES {
loop {
let left = s.len().saturating_sub(letters.len());
let part = &s[left..];
if part == letters {
result += num;
s.truncate(left);
} else {
break;
}
}
}
result
}
fn main() {}
#[cfg(test)]
mod tests {
use crate::roman_to_int;
#[test]
fn test_1() {
assert_eq!(roman_to_int("MMMDCCXLIX".to_string()), 3749);
}
#[test]
fn test_2() {
assert_eq!(roman_to_int("LVIII".to_string()), 58);
}
#[test]
fn test_3() {
assert_eq!(roman_to_int("MCMXCIV".to_string()), 1994);
}
}