-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp13.rs
47 lines (40 loc) · 1 KB
/
p13.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let s1: String = String::from("III");
assert_eq!(roman_to_int(s1), 3);
let s2: String = String::from("IV");
assert_eq!(roman_to_int(s2), 4);
let s3: String = String::from("IX");
assert_eq!(roman_to_int(s3), 9);
}
}
use std::collections::HashMap;
pub fn roman_to_int(s: String) -> i32 {
let map: HashMap<char, i32> = HashMap::from([
('I', 1),
('V', 5),
('X', 10),
('L', 50),
('C', 100),
('D', 500),
('M', 1000),
]);
let s: Vec<char> = s.chars().collect();
let mut ans: i32 = 0;
// [0, len - 1)
for i in 0..(s.len() - 1) {
// if curent >= next
if map.get(&s[i]) >= map.get(&s[i + 1]) {
ans += map.get(&s[i]).unwrap();
}
// else current < next
else {
ans -= map.get(&s[i]).unwrap();
}
}
ans += map.get(&s[s.len() - 1]).unwrap();
ans
}