[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
2024/01/09
👍 一個小小建議,可以用# 或者 ''' 註解''' 在原本的程式碼裡面,會比較容易閱讀和參照。
Youna-avatar-img
發文者
2024/01/10
小松鼠 謝謝你的建議!
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
TOMICA第一波推出吉伊卡哇聯名小車車的時候馬上就被搶購一空,一直很扼腕當時沒有趕緊入手。前陣子閒來無事逛蝦皮,突然發現幾家商場都又開始重新上架,價格也都回到正常水準,估計是官方又再補了一批貨,想都沒想就立刻下單! 同文也跟大家分享近期蝦皮購物紀錄、好用推薦、蝦皮分潤計畫的聯盟行銷!
Thumbnail
TOMICA第一波推出吉伊卡哇聯名小車車的時候馬上就被搶購一空,一直很扼腕當時沒有趕緊入手。前陣子閒來無事逛蝦皮,突然發現幾家商場都又開始重新上架,價格也都回到正常水準,估計是官方又再補了一批貨,想都沒想就立刻下單! 同文也跟大家分享近期蝦皮購物紀錄、好用推薦、蝦皮分潤計畫的聯盟行銷!
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
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