[Leetcode] 13. Roman to Integer

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

題目 : 13. Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD 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.


1.


class Solution:
def romanToInt(self, s: str) -> int:
# 先建立羅馬數字的字典roman
roman = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
#字串是從最後面開始看,所以建立一個 s 的倒序字串 target,方便計算數字的總和(total)
target = s[::-1]
total = 0
if len(target) == 1: # 如果長度是1就可以直接回傳該羅馬字母所代表的數字
total = roman[s]
return total
i = 0
index = len(target)
while(i<len(s)):
#如果目前的羅馬數字>下一個羅馬字母的值,
#就可以將下一個羅馬字母的值減去目前的羅馬數字,再將i+2
if i+1 < index and roman[target[i]] > roman[target[i+1]]:
total = total + roman[target[i]] - roman[target[i+1]]
i = i+2
else:
# 若是其他的狀況則是將目前羅馬字母的值加到total
total = total + roman[target[i]]
i = i+1
return total



avatar-img
7會員
49內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
留言0
查看全部
avatar-img
發表第一個留言支持創作者!
Youna's Devlog 的其他內容
你可能也想看
Google News 追蹤
Thumbnail
/ 大家現在出門買東西還會帶錢包嗎 鴨鴨發現自己好像快一個禮拜沒帶錢包出門 還是可以天天買滿買好回家(? 因此為了記錄手機消費跟各種紅利優惠 鴨鴨都會特別注意銀行的App好不好用! 像是介面設計就是會很在意的地方 很多銀行通常會為了要滿足不同客群 會推出很多App讓使用者下載 每次
給定一個整數 n,請生成所有可能由 n 對括號組成的 有效括號組合。
給定兩個遞增排序的鏈表 list1 和 list2,將它們合併為一個新的 遞增排序 的鏈表,並返回這個新鏈表。新的鏈表應該由這兩個鏈表中的節點組成,而不應創建新的節點。最後回傳合併後的鍊錶的頭節點。
給定一個僅包含字元 '(', ')', '{', '}', '[' 和 ']' 的字串 s,確定輸入字串是否有效。
給定一個Linked list的head,從這個Linked list的末尾刪除第 n 個節點並返回其head。
在這篇文章中,我們將深入剖析 LeetCode 題目 13. Roman to Integer 的解題方法。這是一道經典題目,要求我們將羅馬數字轉換為整數表示。通過學習這篇教學,你將掌握如何處理羅馬數字的規則和高效地實現轉換。
Thumbnail
/ 大家現在出門買東西還會帶錢包嗎 鴨鴨發現自己好像快一個禮拜沒帶錢包出門 還是可以天天買滿買好回家(? 因此為了記錄手機消費跟各種紅利優惠 鴨鴨都會特別注意銀行的App好不好用! 像是介面設計就是會很在意的地方 很多銀行通常會為了要滿足不同客群 會推出很多App讓使用者下載 每次
給定一個整數 n,請生成所有可能由 n 對括號組成的 有效括號組合。
給定兩個遞增排序的鏈表 list1 和 list2,將它們合併為一個新的 遞增排序 的鏈表,並返回這個新鏈表。新的鏈表應該由這兩個鏈表中的節點組成,而不應創建新的節點。最後回傳合併後的鍊錶的頭節點。
給定一個僅包含字元 '(', ')', '{', '}', '[' 和 ']' 的字串 s,確定輸入字串是否有效。
給定一個Linked list的head,從這個Linked list的末尾刪除第 n 個節點並返回其head。
在這篇文章中,我們將深入剖析 LeetCode 題目 13. Roman to Integer 的解題方法。這是一道經典題目,要求我們將羅馬數字轉換為整數表示。通過學習這篇教學,你將掌握如何處理羅馬數字的規則和高效地實現轉換。