DAY 11
0

# 13. Roman to Integer

## Question

Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`. For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.

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:

• `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
• `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
• `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

## Example

### Example1

``````Input: s = "III"
Output: 3
``````

### Example2

``````Input: s = "IV"
Output: 4
``````

### Example3

``````Input: s = "IX"
Output: 9
``````

### Example4

``````Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
``````

### Example5

``````Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
``````

### Constraints

• `1 <= s.length <= 15`
• `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M'`).
• It is guaranteed that `s` is a valid roman numeral in the range `[1, 3999]`.

## 解題

### Think

• 先把str的羅馬字串拆開成一位一位，然後從陣列的後面讀回來，因為要判斷需要加法還是減法。
• 除了讀進來的第一個羅馬數字以外，每個羅馬數字都要判斷他所代表的數字有沒有小於前一個讀進來的羅馬數字，有的話就扣掉，沒有的話就加上去，最後回傳`sum`

### Code

#### Python

``````class Solution:
def romanToInt(self, s: str) -> int:
Symbol_dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}

sum = 0
list_s = list(s)

for index in range(len(list_s)-1, -1, -1):
if index == (len(list_s)-1):
sum += Symbol_dict[list_s[index]]
else:
if Symbol_dict[list_s[index+1]] > Symbol_dict[list_s[index]]:
sum -= Symbol_dict[list_s[index]]
else:
sum += Symbol_dict[list_s[index]]

return sum
``````

#### C

``````int romanToInt(char * s){
int Symbol_dict[] = {1, 5, 10, 50, 100, 500, 1000};
int sum = 0;
int Symbol_dict_index1 = -1, Symbol_dict_index2 = -1;

for(int index=strlen(s)-1; index>=0 ; index--) {
printf("%c\n", s[index]);

if (s[index] == 'I'){
Symbol_dict_index1 = 0;
} else if (s[index] == 'V'){
Symbol_dict_index1 = 1;
} else if (s[index] == 'X'){
Symbol_dict_index1 = 2;
} else if (s[index] == 'L'){
Symbol_dict_index1 = 3;
} else if (s[index] == 'C'){
Symbol_dict_index1 = 4;
} else if (s[index] == 'D'){
Symbol_dict_index1 = 5;
} else if (s[index] == 'M'){
Symbol_dict_index1 = 6;
}

if (index == (strlen(s)-1)){
sum += Symbol_dict[Symbol_dict_index1];
} else {
if (s[index+1] == 'I'){
Symbol_dict_index2 = 0;
} else if (s[index+1] == 'V'){
Symbol_dict_index2 = 1;
} else if (s[index+1] == 'X'){
Symbol_dict_index2 = 2;
} else if (s[index+1] == 'L'){
Symbol_dict_index2 = 3;
} else if (s[index+1] == 'C'){
Symbol_dict_index2 = 4;
} else if (s[index+1] == 'D'){
Symbol_dict_index2 = 5;
} else if (s[index+1] == 'M'){
Symbol_dict_index2 = 6;
}

if (Symbol_dict[Symbol_dict_index2] > Symbol_dict[Symbol_dict_index1]){
sum -= Symbol_dict[Symbol_dict_index1];
} else {
sum += Symbol_dict[Symbol_dict_index1];
}
}

}
return sum;

}
``````

### Result

• Python

• • C

• 30天 Leetcode解題之路30