[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
想在蝦皮雙11買到最划算?這篇文章將分享作者精選的蝦皮高CP值商品,包含HERAN禾聯冷氣、HITACHI日立冰箱、DJI無線麥克風、FUJIFILM拍立得,並提供蝦皮雙11優惠券領取教學、省錢技巧,以及蝦皮分潤計畫介紹,讓你買得開心、省得多!
Thumbnail
想在蝦皮雙11買到最划算?這篇文章將分享作者精選的蝦皮高CP值商品,包含HERAN禾聯冷氣、HITACHI日立冰箱、DJI無線麥克風、FUJIFILM拍立得,並提供蝦皮雙11優惠券領取教學、省錢技巧,以及蝦皮分潤計畫介紹,讓你買得開心、省得多!
Thumbnail
2025 蝦皮 1111 購物節又來了!分享三大必買原因:全站 $0 起免運、多重優惠疊加、便利取貨。 此外,推薦兩款高 CP 值的即食拉麵(無印良品即食迷你拉麵、維力迷你麵野菜拉麵),並分享如何透過「蝦皮分潤計畫」放大效益,開心購物之餘還能獲得額外收益!
Thumbnail
2025 蝦皮 1111 購物節又來了!分享三大必買原因:全站 $0 起免運、多重優惠疊加、便利取貨。 此外,推薦兩款高 CP 值的即食拉麵(無印良品即食迷你拉麵、維力迷你麵野菜拉麵),並分享如何透過「蝦皮分潤計畫」放大效益,開心購物之餘還能獲得額外收益!
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—算幾不等式
Thumbnail
高中數學主題練習—算幾不等式
Thumbnail
手寫版,有寫錯或看不懂的地方,都可以在底下留言給我。 感謝!
Thumbnail
手寫版,有寫錯或看不懂的地方,都可以在底下留言給我。 感謝!
Thumbnail
高中數學主題練習—相關係數計算
Thumbnail
高中數學主題練習—相關係數計算
Thumbnail
題目 : 13. Roman to Integer
Thumbnail
題目 : 13. Roman to Integer
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News