|
| 1 | +/** |
| 2 | + * [13] 罗马数字转整数 |
| 3 | + * |
| 4 | + * 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 |
| 5 | + * |
| 6 | + * 字符 数值 |
| 7 | + * I 1 |
| 8 | + * V 5 |
| 9 | + * X 10 |
| 10 | + * L 50 |
| 11 | + * C 100 |
| 12 | + * D 500 |
| 13 | + * M 1000 |
| 14 | + * 例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。 |
| 15 | + * 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况: |
| 16 | + * |
| 17 | + * I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。 |
| 18 | + * X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 |
| 19 | + * C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。 |
| 20 | + * |
| 21 | + * 给定一个罗马数字,将其转换成整数。 |
| 22 | + * |
| 23 | + * 示例 1: |
| 24 | + * |
| 25 | + * 输入: s = "III" |
| 26 | + * 输出: 3 |
| 27 | + * 示例 2: |
| 28 | + * |
| 29 | + * 输入: s = "IV" |
| 30 | + * 输出: 4 |
| 31 | + * 示例 3: |
| 32 | + * |
| 33 | + * 输入: s = "IX" |
| 34 | + * 输出: 9 |
| 35 | + * 示例 4: |
| 36 | + * |
| 37 | + * 输入: s = "LVIII" |
| 38 | + * 输出: 58 |
| 39 | + * 解释: L = 50, V= 5, III = 3. |
| 40 | + * |
| 41 | + * 示例 5: |
| 42 | + * |
| 43 | + * 输入: s = "MCMXCIV" |
| 44 | + * 输出: 1994 |
| 45 | + * 解释: M = 1000, CM = 900, XC = 90, IV = 4. |
| 46 | + * |
| 47 | + * 提示: |
| 48 | + * |
| 49 | + * 1 <= s.length <= 15 |
| 50 | + * s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M') |
| 51 | + * 题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内 |
| 52 | + * 题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。 |
| 53 | + * IL 和 IM 这样的例子并不符合题目要求,49 应该写作 XLIX,999 应该写作 CMXCIX 。 |
| 54 | + * 关于罗马数字的详尽书写规则,可以参考 <a href="https://b2b.partcommunity.com/community/knowledge/zh_CN/detail/10753/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97#knowledge_article">罗马数字 - Mathematics </a>。 |
| 55 | + * |
| 56 | + */ |
| 57 | +pub struct Solution {} |
| 58 | + |
| 59 | +// problem: https://leetcode.cn/problems/roman-to-integer/ |
| 60 | +// discuss: https://leetcode.cn/problems/roman-to-integer/discuss/?currentPage=1&orderBy=most_votes&query= |
| 61 | + |
| 62 | +// submission codes start here |
| 63 | + |
| 64 | +impl Solution { |
| 65 | + pub fn roman_to_int(s: String) -> i32 { |
| 66 | + let mut p = s.chars().peekable(); |
| 67 | + let mut res = 0; |
| 68 | + loop { |
| 69 | + match p.next() { |
| 70 | + Some('M') => { |
| 71 | + res += 1000; |
| 72 | + }, |
| 73 | + Some('D') => { |
| 74 | + res += 500; |
| 75 | + } |
| 76 | + Some('C') => { |
| 77 | + match p.peek() { |
| 78 | + Some('M') => { |
| 79 | + res += 900; |
| 80 | + p.next(); |
| 81 | + }, |
| 82 | + Some('D') => { |
| 83 | + res += 400; |
| 84 | + p.next(); |
| 85 | + }, |
| 86 | + _ => { |
| 87 | + res += 100; |
| 88 | + }, |
| 89 | + } |
| 90 | + }, |
| 91 | + Some('L') => { |
| 92 | + res += 50; |
| 93 | + }, |
| 94 | + Some('X') => { |
| 95 | + match p.peek() { |
| 96 | + Some('C') => { |
| 97 | + res += 90; |
| 98 | + p.next(); |
| 99 | + }, |
| 100 | + Some('L') => { |
| 101 | + res += 40; |
| 102 | + p.next(); |
| 103 | + }, |
| 104 | + _ => { |
| 105 | + res += 10; |
| 106 | + }, |
| 107 | + } |
| 108 | + }, |
| 109 | + Some('V') => { |
| 110 | + res += 5; |
| 111 | + }, |
| 112 | + Some('I') => { |
| 113 | + match p.peek() { |
| 114 | + Some('X') => { |
| 115 | + res += 9; |
| 116 | + p.next(); |
| 117 | + }, |
| 118 | + Some('V') => { |
| 119 | + res += 4; |
| 120 | + p.next(); |
| 121 | + }, |
| 122 | + _ => { |
| 123 | + res += 1; |
| 124 | + }, |
| 125 | + } |
| 126 | + }, |
| 127 | + _ => { |
| 128 | + return res; |
| 129 | + }, |
| 130 | + } |
| 131 | + } |
| 132 | + res |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// submission codes end |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +mod tests { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_13() { |
| 144 | + assert_eq!(Solution::roman_to_int("M".to_string()), 1000); |
| 145 | + assert_eq!(Solution::roman_to_int("MM".to_string()), 2000); |
| 146 | + assert_eq!(Solution::roman_to_int("LVIII".to_string()), 58); |
| 147 | + assert_eq!(Solution::roman_to_int("IX".to_string()), 9); |
| 148 | + assert_eq!(Solution::roman_to_int("IV".to_string()), 4); |
| 149 | + assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994); |
| 150 | + } |
| 151 | +} |
0 commit comments