[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
留言分享你的想法!
avatar-img
Youna's Devlog
7會員
49內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
Youna's Devlog的其他內容
2024/02/13
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
2024/02/13
題目 : 121. Best Time to Buy and Sell Stock
Thumbnail
2024/01/11
題目 : 100. Same Tree
Thumbnail
2024/01/11
題目 : 100. Same Tree
Thumbnail
2024/01/11
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
2024/01/11
題目 : 83. Remove Duplicates from Sorted List
Thumbnail
看更多
你可能也想看
Thumbnail
嗨大家好!我是重度網購患者賴床鴨鴨!每年最期待的蝦皮12.12又要來了,我的購物車也早已瀕臨爆炸邊緣。 尤其是我最近在做的「高速負離子吹風機」評比,一口氣比較了5款,每台都想帶回家!但身為一個精打細算的小資族,我發現了一個天大的祕密:原來在蝦皮購物,居然可以不只省錢,還能偷偷賺錢! 今天這篇,我
Thumbnail
嗨大家好!我是重度網購患者賴床鴨鴨!每年最期待的蝦皮12.12又要來了,我的購物車也早已瀕臨爆炸邊緣。 尤其是我最近在做的「高速負離子吹風機」評比,一口氣比較了5款,每台都想帶回家!但身為一個精打細算的小資族,我發現了一個天大的祕密:原來在蝦皮購物,居然可以不只省錢,還能偷偷賺錢! 今天這篇,我
Thumbnail
又到了每年最溫(ㄏㄨㄚ)馨(ㄑㄧㄢˊ)的季節,其實在還單身的時候,我並沒有特別喜愛這個節日,但自從去了歐洲生活,再加上有了孩子後,對於聖誕節的好感與日俱增,除了要幫孩子準備聖誕禮物,還需要幫家佈置一下才有儀式感,若你對於今年的聖誕禮物還沒有頭緒?趁著蝦皮雙12的優惠,快來看看我買了什麼吧!
Thumbnail
又到了每年最溫(ㄏㄨㄚ)馨(ㄑㄧㄢˊ)的季節,其實在還單身的時候,我並沒有特別喜愛這個節日,但自從去了歐洲生活,再加上有了孩子後,對於聖誕節的好感與日俱增,除了要幫孩子準備聖誕禮物,還需要幫家佈置一下才有儀式感,若你對於今年的聖誕禮物還沒有頭緒?趁著蝦皮雙12的優惠,快來看看我買了什麼吧!
Thumbnail
來了來了~~~購物狂最期待的大日子真的就是-蝦皮 12.12,折扣真的一年比一年還誇張,價格越殺越狠,各種免運、滿額折、品牌券直接發到領不完! 從這裡加入我的分潤連結:https://s.shopee.tw/7fSd8GVvNC 推薦碼請填:A6ZVDYE
Thumbnail
來了來了~~~購物狂最期待的大日子真的就是-蝦皮 12.12,折扣真的一年比一年還誇張,價格越殺越狠,各種免運、滿額折、品牌券直接發到領不完! 從這裡加入我的分潤連結:https://s.shopee.tw/7fSd8GVvNC 推薦碼請填:A6ZVDYE
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—算幾不等式
Thumbnail
高中數學主題練習—算幾不等式
Thumbnail
手寫版,有寫錯或看不懂的地方,都可以在底下留言給我。 感謝!
Thumbnail
手寫版,有寫錯或看不懂的地方,都可以在底下留言給我。 感謝!
Thumbnail
高中數學主題練習—相關係數計算
Thumbnail
高中數學主題練習—相關係數計算
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News