|
| 1 | +# 13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions |
| 2 | + |
| 3 | +Visit original link: [13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/13-roman-to-integer) for a better experience! |
| 4 | + |
| 5 | +LeetCode link: [13. Roman to Integer](https://leetcode.com/problems/roman-to-integer), difficulty: **Easy**. |
| 6 | + |
| 7 | +## LeetCode description of "13. Roman to Integer" |
| 8 | + |
| 9 | +Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`. |
| 10 | + |
| 11 | +``` |
| 12 | +Symbol Value |
| 13 | +I 1 |
| 14 | +V 5 |
| 15 | +X 10 |
| 16 | +L 50 |
| 17 | +C 100 |
| 18 | +D 500 |
| 19 | +M 1000 |
| 20 | +``` |
| 21 | + |
| 22 | +For example, `2` is written as `II` in Roman numeral, just two ones added together. `12` is written as `XII`, which is simply `X` + `II`. The number `27` is written as `XXVII`, which is `XX` + `V` + `II`. |
| 23 | + |
| 24 | +Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used: |
| 25 | + |
| 26 | +`I` can be placed before `V` (5) and `X` (10) to make 4 and 9. |
| 27 | +`X` can be placed before `L` (50) and `C` (100) to make 40 and 90. |
| 28 | +`C` can be placed before `D` (500) and `M` (1000) to make 400 and 900. |
| 29 | +Given a roman numeral, convert it to an integer. |
| 30 | + |
| 31 | +### [Example 1] |
| 32 | + |
| 33 | +**Input**: `s = "III"` |
| 34 | + |
| 35 | +**Output**: `3` |
| 36 | + |
| 37 | +### [Example 2] |
| 38 | + |
| 39 | +**Input**: `s = "IV"` |
| 40 | + |
| 41 | +**Output**: `4` |
| 42 | + |
| 43 | +### [Example 3] |
| 44 | + |
| 45 | +**Input**: `s = "IX"` |
| 46 | + |
| 47 | +**Output**: `9` |
| 48 | + |
| 49 | +### [Example 4] |
| 50 | + |
| 51 | +**Input**: `s = "LVIII"` |
| 52 | + |
| 53 | +**Output**: `58` |
| 54 | + |
| 55 | +**Explanation**: `L = 50, V= 5, III = 3.` |
| 56 | + |
| 57 | +### [Example 5] |
| 58 | + |
| 59 | +**Input**: `s = "MCMXCIV"` |
| 60 | + |
| 61 | +**Output**: `1994` |
| 62 | + |
| 63 | +**Explanation**: `M = 1000, CM = 900, XC = 90, IV = 4.` |
| 64 | + |
| 65 | +### [Constraints] |
| 66 | + |
| 67 | +- `1 <= s.length <= 15` |
| 68 | +- `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`. |
| 69 | +- It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`. |
| 70 | + |
| 71 | +### [Hints] |
| 72 | + |
| 73 | +<details> |
| 74 | + <summary>Hint 1</summary> |
| 75 | + Problem is simpler to solve by working the string from back to front and using a map. |
| 76 | + |
| 77 | + |
| 78 | +</details> |
| 79 | + |
| 80 | +## Intuition |
| 81 | + |
| 82 | +* The correspondence between characters and values can be represented with a `Map`. |
| 83 | +* The intuition is to add the value to `result` whenever a digit is encountered. |
| 84 | +* But cases like `IV` may occur, so you need to consider whether to process from left to right or from right to left. Which processing direction do you choose? |
| 85 | + <details><summary>Click to view the answer</summary><p> Processing from right to left is more convenient, because once you see that the current character and the previous character form a specific combination, you can handle it directly. </p></details> |
| 86 | +* How do you deal with cases like `IV`? |
| 87 | + <details><summary>Click to view the answer</summary><p> Just handle it in reverse. In the forward direction you do addition, but now you do subtraction. </p></details> |
| 88 | + |
| 89 | +## Complexity |
| 90 | + |
| 91 | +- Time complexity: `O(N)`. |
| 92 | +- Space complexity: `O(1)`. |
| 93 | + |
| 94 | +## Ruby |
| 95 | + |
| 96 | +```ruby |
| 97 | +# @param {String} s |
| 98 | +# @return {Integer} |
| 99 | +def roman_to_int(s) |
| 100 | + symbol_to_value = { |
| 101 | + 'I' => 1, |
| 102 | + 'V' => 5, |
| 103 | + 'X' => 10, |
| 104 | + 'L' => 50, |
| 105 | + 'C' => 100, |
| 106 | + 'D' => 500, |
| 107 | + 'M' => 1000, |
| 108 | + } |
| 109 | + result = 0 |
| 110 | + previous_char = nil |
| 111 | + |
| 112 | + (s.size - 1).downto(0).each do |i| |
| 113 | + char = s[i] |
| 114 | + if ('I' == char && ['V', 'X'].include?(previous_char)) || |
| 115 | + ('X' == char && ['L', 'C'].include?(previous_char)) || |
| 116 | + ('C' == char && ['D', 'M'].include?(previous_char)) |
| 117 | + result -= symbol_to_value[char] |
| 118 | + else |
| 119 | + result += symbol_to_value[char] |
| 120 | + end |
| 121 | + previous_char = char |
| 122 | + end |
| 123 | + |
| 124 | + result |
| 125 | +end |
| 126 | +``` |
| 127 | + |
| 128 | +## Python |
| 129 | + |
| 130 | +```python |
| 131 | +class Solution: |
| 132 | + def romanToInt(self, s: str) -> int: |
| 133 | + symbol_to_value = { |
| 134 | + 'I': 1, |
| 135 | + 'V': 5, |
| 136 | + 'X': 10, |
| 137 | + 'L': 50, |
| 138 | + 'C': 100, |
| 139 | + 'D': 500, |
| 140 | + 'M': 1000, |
| 141 | + } |
| 142 | + result = 0 |
| 143 | + previous_char = None |
| 144 | + |
| 145 | + for i in range(len(s) - 1, -1, -1): |
| 146 | + char = s[i] |
| 147 | + |
| 148 | + if ('I' == char and previous_char in ['V', 'X']) or \ |
| 149 | + ('X' == char and previous_char in ['L', 'C']) or \ |
| 150 | + ('C' == char and previous_char in ['D', 'M']): |
| 151 | + result -= symbol_to_value[char] |
| 152 | + else: |
| 153 | + result += symbol_to_value[char] |
| 154 | + |
| 155 | + previous_char = char |
| 156 | + |
| 157 | + return result |
| 158 | +``` |
| 159 | + |
| 160 | +## Other languages |
| 161 | + |
| 162 | +```java |
| 163 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 164 | +``` |
| 165 | + |
| 166 | +Dear LeetCoders! For a better LeetCode problem-solving experience, please visit website [LeetCode.to](https://leetcode.to): Dare to claim the best practices of LeetCode solutions! Will save you a lot of time! |
| 167 | + |
| 168 | +Original link: [13. Roman to Integer - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/13-roman-to-integer). |
| 169 | + |
| 170 | +GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java). |
0 commit comments