-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0013-roman-to-integer.rs
47 lines (43 loc) · 1.13 KB
/
0013-roman-to-integer.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
impl Solution {
pub fn roman_to_int(s: String) -> i32 {
let s: Vec<char> = s.chars().collect();
let mut res = 0;
for i in 0..s.len() {
if i + 1 < s.len() && Self::get_value(s[i]) < Self::get_value(s[i + 1]) {
res -= Self::get_value(s[i]);
} else {
res += Self::get_value(s[i]);
}
}
res
}
pub fn get_value(ch: char) -> i32 {
match ch {
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
_ => 0,
}
}
pub fn roman_to_int_functional(s: String) -> i32 {
s.chars().rfold(0, |acc, ch| {
acc + match ch {
'I' if acc >= 5 => -1,
'I' => 1,
'V' => 5,
'X' if acc >= 50 => -10,
'X' => 10,
'L' => 50,
'C' if acc >= 500 => -100,
'C' => 100,
'D' => 500,
'M' => 1000,
_ => 0,
}
})
}
}