-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroman-to-integer.rs
72 lines (65 loc) · 1.88 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(3, Solution::roman_to_int("III".to_string()));
assert_eq!(4, Solution::roman_to_int("IV".to_string()));
assert_eq!(9, Solution::roman_to_int("IX".to_string()));
assert_eq!(58, Solution::roman_to_int("LVIII".to_string()));
assert_eq!(1994, Solution::roman_to_int("MCMXCIV".to_string()));
}
struct Solution;
impl Solution {
pub fn roman_to_int(s: String) -> i32 {
let mut result = 0;
let s = s.as_bytes();
for (i, &v) in s.iter().enumerate() {
let m = match v {
b'I' => 1,
b'V' => {
if i != 0 && s[i - 1] == b'I' {
3
} else {
5
}
}
b'X' => {
if i != 0 && s[i - 1] == b'I' {
8
} else {
10
}
}
b'L' => {
if i != 0 && s[i - 1] == b'X' {
30
} else {
50
}
}
b'C' => {
if i != 0 && s[i - 1] == b'X' {
80
} else {
100
}
}
b'D' => {
if i != 0 && s[i - 1] == b'C' {
300
} else {
500
}
}
b'M' => {
if i != 0 && s[i - 1] == b'C' {
800
} else {
1000
}
}
_ => 0,
};
result += m;
}
result
}
}