【LittleDuck_LeetCodeNote】13 - Roman to Integer

更新於 發佈於 閱讀時間約 13 分鐘

【Instagram】 / 【巴哈姆特小屋】 / 【Vocus方格子】 / 【Medium】
喜歡的話就按個愛心或贊助ㄅ。


raw-image

Question and Hints

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 = 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].

First Solution

💡 用switch-case從第一個字母開始找,
用對應的文字找出這一輪對應的的Index,記錄為nowIndex,
因為羅馬數字有將小字母擺在大字母左邊表示相減的特性,
所以檢查上一個字母(lastIndex)是否小於nowIndex,
如果是,就先相減,再做相加,
如果不是就直接加。
class Solution {
public int romanToInt(String s) {
char[] RomanNumber = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int[] RomanValue = {1, 5, 10, 50, 100, 500, 1000};
int lastIndex = 0, nowIndex = 0, result = 0;

for(int i=0; i < s.length(); i++){
switch(s.charAt(i)){
case 'I':
nowIndex = 0;
break;
case 'V':
nowIndex = 1;
break;
case 'X':
nowIndex = 2;
break;
case 'L':
nowIndex = 3;
break;
case 'C':
nowIndex = 4;
break;
case 'D':
nowIndex = 5;
break;
case 'M':
nowIndex = 6;
break;
}
if(i == 0)
lastIndex = nowIndex;
if(lastIndex < nowIndex){
result -= RomanValue[lastIndex] * 2;
}
result += RomanValue[nowIndex];
lastIndex = nowIndex;
}

return result;
}
}

⏰ Runtime: 4 ms (95.38 %)

📦 Memory: 42.8 MB (57.74 %)



Upgrade Solution

more clear.

💡 First Solution邏輯大致上對了,但可以改得更精簡, 並且,順序反過來會更好找。
  • 不用2個陣列,而是直接在switch-case完成羅馬數字的參照轉換(透過number)
  • 因為不用陣列,所以不用Index,而是直接用字母代表的數字進行比較。
  • 「小字母擺在大字母左邊表示相減的特性」從數學角度來看,即當由右往左看時,下一個數字如果比較小,就要進行相減,比起原本相減2次更直觀。
  • JDK14更新了switch-case的寫法,改用 -> 取代 : ,且結尾不用再寫break。
class Solution {
public int romanToInt(String s) {
int answer = 0, number = 0, prev = 0;

for (int j = s.length()-1; j >= 0; j--) {
switch (s.charAt(j)) {
case 'M' -> number = 1000;
case 'D' -> number = 500;
case 'C' -> number = 100;
case 'L' -> number = 50;
case 'X' -> number = 10;
case 'V' -> number = 5;
case 'I' -> number = 1;
}
if (number < prev)
answer -= number;
else
answer += number;
prev = number;
}
return answer;
}
}

⏰ Runtime: 3 ms (100 %)

📦 Memory: 42.9 MB (57.74 %)



Bonus Solution

Use Hashtable to store Roman numbers.

💡 與First Solution的邏輯相同,差別在於用Hashtable替代兩個陣列,
結果來看明顯比較慢,
其實兩個陣列的搜尋方式就跟Hashtable的道理是一樣的,
只是把key跟value寫出來,
並且中間沒有Hash function轉換,
所以兩個陣列反而比較快,
但HashTable還是有安全性上的優點。
class Solution {
public int romanToInt(String s) {
Map<Character,Integer>map = new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);

int value=0;
char prevVal = 'I';
for(int i=s.length()-1; i>=0; i--){
char nowVal = s.charAt(i);
if(map.get(nowVal) < map.get(prevVal))
value -= map.get(nowVal);
else
value += map.get(nowVal);
prevVal = nowVal;
}
return value;
}
}

⏰ Runtime: 7 ms (35.9 %)

📦 Memory: 42.6 MB (82.32 %)




留言
avatar-img
留言分享你的想法!
avatar-img
Steven SHIH的沙龍
6會員
36內容數
以英文和日文歌的翻譯為主,並從「歌曲裡的故事」這個角度去翻譯。畢竟自己只有中文算好而已 :D
Steven SHIH的沙龍的其他內容
2023/07/10
Find the Index of the First Occurrence in a String : 找出haystack在字串中第一次出現的index.
Thumbnail
2023/07/10
Find the Index of the First Occurrence in a String : 找出haystack在字串中第一次出現的index.
Thumbnail
2023/07/10
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
2023/07/10
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
2023/07/10
Remove Duplicates from Sorted Array : 將陣列中的重複值移除,並回傳陣列裡的元素總數。
Thumbnail
2023/07/10
Remove Duplicates from Sorted Array : 將陣列中的重複值移除,並回傳陣列裡的元素總數。
Thumbnail
看更多
你可能也想看
Thumbnail
2025 vocus 推出最受矚目的活動之一——《開箱你的美好生活》,我們跟著創作者一起「開箱」各種故事、景點、餐廳、超值好物⋯⋯甚至那些讓人會心一笑的生活小廢物;這次活動不僅送出了許多獎勵,也反映了「內容有價」——創作不只是分享、紀錄,也能用各種不同形式變現、帶來實際收入。
Thumbnail
2025 vocus 推出最受矚目的活動之一——《開箱你的美好生活》,我們跟著創作者一起「開箱」各種故事、景點、餐廳、超值好物⋯⋯甚至那些讓人會心一笑的生活小廢物;這次活動不僅送出了許多獎勵,也反映了「內容有價」——創作不只是分享、紀錄,也能用各種不同形式變現、帶來實際收入。
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目會給定我們一組十進位表示法的阿拉伯數字,要求我們把它轉換為對應的羅馬數字。
Thumbnail
題目會給定我們一組十進位表示法的阿拉伯數字,要求我們把它轉換為對應的羅馬數字。
Thumbnail
題目會給定我們一組羅馬數字,要求我們把它轉換為對應十進位表示法的阿拉伯數字。
Thumbnail
題目會給定我們一組羅馬數字,要求我們把它轉換為對應十進位表示法的阿拉伯數字。
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
Roman to Integer : 將羅馬數字轉換成阿拉伯數字。
Thumbnail
Roman to Integer : 將羅馬數字轉換成阿拉伯數字。
Thumbnail
Palindrome Number : 判斷x是否為迴文 ( 左到右,右到左,內容皆相同 ) ,若符合回傳true,不符合則回傳false。
Thumbnail
Palindrome Number : 判斷x是否為迴文 ( 左到右,右到左,內容皆相同 ) ,若符合回傳true,不符合則回傳false。
Thumbnail
這只是一篇測試功能的文章
Thumbnail
這只是一篇測試功能的文章
Thumbnail
半自學幾天後,終於才進到書中的迴圈!         前後也相繼完成朋友出的作業,實在是萬分感謝他,我也完成了幾個迴圈的小作業,然後又接收到一個要把「阿拉伯數字」變成「中文字」的作業,譬如: 輸入1042顯示一千零四十二。這個我懂,我可是有教過小朋友數學好幾年的老師,另一個學生常見的問題就是1003
Thumbnail
半自學幾天後,終於才進到書中的迴圈!         前後也相繼完成朋友出的作業,實在是萬分感謝他,我也完成了幾個迴圈的小作業,然後又接收到一個要把「阿拉伯數字」變成「中文字」的作業,譬如: 輸入1042顯示一千零四十二。這個我懂,我可是有教過小朋友數學好幾年的老師,另一個學生常見的問題就是1003
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News