-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0013_roman_to_integer.rs
More file actions
43 lines (38 loc) · 1.21 KB
/
s0013_roman_to_integer.rs
File metadata and controls
43 lines (38 loc) · 1.21 KB
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
#![allow(unused)]
pub struct Solution {}
use std::collections::HashMap;
impl Solution {
// O(1) O(1)
pub fn roman_to_int(s: String) -> i32 {
let mut map = HashMap::new();
map.insert('I', 1);
map.insert('V', 5);
map.insert('X', 10);
map.insert('L', 50);
map.insert('C', 100);
map.insert('D', 500);
map.insert('M', 1000);
let chs = s.chars().collect::<Vec<char>>();
let mut total = *map.get(&chs[chs.len() - 1]).unwrap();
for idx in (0..chs.len() - 1).rev() {
if *map.get(&chs[idx]).unwrap() < *map.get(&chs[idx + 1]).unwrap() {
total -= *map.get(&chs[idx]).unwrap();
} else {
total += *map.get(&chs[idx]).unwrap();
}
}
total
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_13() {
assert_eq!(Solution::roman_to_int("III".to_string()), 3);
assert_eq!(Solution::roman_to_int("IV".to_string()), 4);
assert_eq!(Solution::roman_to_int("IX".to_string()), 9);
assert_eq!(Solution::roman_to_int("LVIII".to_string()), 58);
assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994);
}
}