-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.罗马数字转整数.rs
43 lines (38 loc) · 882 Bytes
/
13.罗马数字转整数.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
/*
* @lc app=leetcode.cn id=13 lang=rust
*
* [13] 罗马数字转整数
*/
// @lc code=start
use std::collections::HashMap;
impl Solution {
pub fn roman_to_int(s: String) -> i32 {
let len = s.len();
let mut pre = 0;
let mut res = 0;
for i in 0..len {
let a = &s[i..i + 1];
let num = match a {
"I" => 1,
"V" => 5,
"X" => 10,
"L" => 50,
"C" => 100,
"D" => 500,
"M" => 1000,
_ => 0,
};
if pre == 0 {
pre = num;
} else if pre < num {
res += (num - pre);
pre = 0;
} else {
res += pre;
pre = num;
}
}
res + pre
}
}
// @lc code=end