leetCode EASY - Q13. Roman to Integer
===

---
###### tags: `leetCode`, `練習刷題`, `219`
<br>
## 題目
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
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.
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 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV =
---
## 講解:
這個題目呢,就是上面給妳一組羅馬數字的對比。然後利用那組格式去做篩選後,做個總合。
## 我的解法:
```
var romanToInt = function(s) {
const romanMap={
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
let total =0;
let prevValue = 0;
for(let i=s.length -1; i >=0 ; i--){
const currentValue = romanMap[s[i]];
if(currentValue >= prevValue){
total+= currentValue;
}else {
total-=currentValue;
}
prevValue=currentValue;
}
return total;
};
```
1. 先依照以上的格式建立好,然後初始化 total 和 prevValue。
2. 這裡用迴圈,可以用從左到右,也可以從右到左。
3. 建立 currentValue 紀錄現在的string。romanMap作為資料格式,然後用s作為現在的值,s[i]意思是s.length 第幾個這樣。如果目前s 是 “MCMXCIV”,然後現在的迴圈是逆向的,那 s[0]就是 “V”,然後再去對比,所以 currentValue = 5.
4. 如果目前的數字比之前的數字大,那就加入total裡,反則刪減total裡的數字。
5. 最後統計total。
6. 演算記過如下:
- 字符 V
currentValue = romanMap['V'] = 5
prevValue = 0 (因為這是第一個字符)
因為 currentValue >= prevValue,所以 total += 5
更新 prevValue 為 5
- 字符 I
currentValue = romanMap['I'] = 1
prevValue = 5
因為 currentValue < prevValue,所以 total -= 1
更新 prevValue 為 1
- 字符 C
currentValue = romanMap['C'] = 100
prevValue = 1
因為 currentValue >= prevValue,所以 total += 100
更新 prevValue 為 100
- 字符 X
currentValue = romanMap['X'] = 10
prevValue = 100
因為 currentValue < prevValue,所以 total -= 10
更新 prevValue 為 10
- 字符 C
currentValue = romanMap['C'] = 100
prevValue = 10
因為 currentValue >= prevValue,所以 total += 100
更新 prevValue 為
- 字符 M
currentValue = romanMap['M'] = 1000
prevValue = 100
因為 currentValue >= prevValue,所以 total += 1000
更新 prevValue 為 1000
- 結果計算:
最終 total = 5 - 1 + 100 - 10 + 100 + 1000 = 1194
---
## 別人的解題:
```
const algorism = new Map([
['CM', 900],
['CD', 400],
['XC', 90],
['XL', 40],
['IX', 9],
['IV', 4],
['M', 1000],
['D', 500],
['C', 100],
['L', 50],
['X', 10],
['V', 5],
['I', 1],
]);
function romanToInt(s) {
let acumulator = 0;
for (let i = 0; i < s.length; i++) {
const twoCharSymbol = s[i] + s[i + 1];
const oneCharSymbol = s[i];
if (algorism.has(twoCharSymbol)) {
acumulator += algorism.get(twoCharSymbol);
i++;
} else {
acumulator += algorism.get(oneCharSymbol);
}
}
return acumulator;
}
```
- “alogorism” 是一個 Map,它對應羅馬數字到阿拉伯數字值。
- "romanInput" 是講羅馬數字轉換為整數的函式。
#### 雙字符檢查:
- 每次迭代,函數首先檢查當前字符與下一個字符組成的兩個字符('twoCharSymbol'),例如,如果當前字符是 "C" 並且下一個字符 "M" ,則是 "twoCharSymbol" 就是 "CM"
- 如果這個兩字符組合存在於 "algorism" Map 中,說明它是一個有效的羅馬數字組合,這通常表示一個減法操作,如 "CM" 代表900。如果找到這樣的組合,則將對應的值加到 "acumulator", 並將索引 i 增加一個額外的,因為這兩個字符已經一起處理了。
#### 單字符檢查:
- 如果上訴的兩字符組合不在Map中,則只考慮當前字符 "oneCharSymbol" 。這個字符應該是一個有效的羅馬數字,如 "I, V" 等。
- 講這個字符對應的值加到 "acumulator".
#### 循環結束:
- 當 for 循環完成後,所有的羅馬數字字符都被轉成對應的數值並夾到 "acumulator"
- 最後,函數返回 "acumulator" 的值,即輸入羅馬數字字符串的整數表示。
- i=0, 'M' => 1000 加到 acumulator
- i=1, 'CM' => 900 加到 acumulator,i 增加額外的 1(跳過 'M')
- i=3, 'XC' => 90 加到 acumulator,i 增加額外的 1(跳過 'C')
- i=5, 'IV' => 4 加到 acumulator,i 增加額外的 1(跳過 'I')
-
最後,acumulator 的值是 1994,這是 "MCMXCIV" 的整數表示。