-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
67 lines (63 loc) · 2.3 KB
/
main.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
fn main() {
println!("Hello, world!");
Solution::roman_to_int(String::from("III"));
}
struct Solution {}
impl Solution {
pub fn roman_to_int(s: String) -> i32 {
let mut ret = 0;
let mut iter = s.bytes();
let mut previous = iter.next();
loop {
let current = iter.next();
match (previous, current) {
(Some(b'I'), Some(b'V')) => { ret += 4; previous = iter.next() }
(Some(b'I'), Some(b'X')) => { ret += 9; previous = iter.next() }
(Some(b'X'), Some(b'L')) => { ret += 40; previous = iter.next() }
(Some(b'X'), Some(b'C')) => { ret += 90; previous = iter.next() }
(Some(b'C'), Some(b'D')) => { ret += 400; previous = iter.next() }
(Some(b'C'), Some(b'M')) => { ret += 900; previous = iter.next() }
(None, None) => break ret,
(Some(c), None) => {
match c {
b'I' => ret += 1,
b'V' => ret += 5,
b'X' => ret += 10,
b'L' => ret += 50,
b'C' => ret += 100,
b'D' => ret += 500,
b'M' => ret += 1000,
_ => ()
}
break ret;
}
(Some(c), Some(r)) => {
match c {
b'I' => ret += 1,
b'V' => ret += 5,
b'X' => ret += 10,
b'L' => ret += 50,
b'C' => ret += 100,
b'D' => ret += 500,
b'M' => ret += 1000,
_ => ()
}
previous = Some(r);
}
(None, Some(_)) => ()
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic() {
assert_eq!(Solution::roman_to_int(String::from("III")), 3);
assert_eq!(Solution::roman_to_int(String::from("IV")), 4);
assert_eq!(Solution::roman_to_int(String::from("IX")), 9);
assert_eq!(Solution::roman_to_int(String::from("LVIII")), 58);
assert_eq!(Solution::roman_to_int(String::from("MCMXCIV")), 1994);
}
}