題目如下:
這道題提供兩種思路,一種是用判斷語句窮舉所有情況,參考代碼如下:
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman = s
num = 0
rlen = len(roman)
if rlen == 0:
return 0
i = 0
while i < rlen:
if roman[i] == "M":
num = num + 1000
i = i + 1
continue
if roman[i] == "C":
if i + 1 < rlen:
if roman[i+1] == "M":
num = num + 900
i = i + 2
continue
elif roman[i+1] == "D":
num = num + 400
i = i + 2
continue
else:
num = num + 100
i = i + 1
continue
else:
num = num + 100
i = i + 1
continue
if roman[i] == "D":
num = num + 500
i = i + 1
continue
if roman[i] == "X":
if i + 1 < rlen:
if roman[i+1] == "C":
num = num + 90
i = i + 2
continue
elif roman[i+1] == "L":
num = num + 40
i = i + 2
continue
else:
num = num + 10
i = i + 1
continue
else:
num = num + 10
i = i + 1
continue
if roman[i] == "L":
num = num + 50
i = i + 1
continue
if roman[i] == "I":
if i + 1 < rlen:
if roman[i+1] == "X":
num = num + 9
i = i + 2
continue
elif roman[i+1] == "V":
num = num + 4
i = i + 2
continue
else:
num = num + 1
i = i + 1
continue
else:
num = num + 1
i = i + 1
continue
if roman[i] == "V":
num = num + 5
i = i + 1
continue
return num
上面這種解法代碼有點繁復(fù)谦疾,下面提供另一種解法励稳,通過對于的羅馬數(shù)字字典來倒序處理字符串佃乘,值得注意的是,如果當(dāng)前字符比上次迭代的字符所代表的數(shù)字要小驹尼,那么應(yīng)該從總數(shù)中減去趣避。如“CD,IV”扶欣。參考代碼如下:
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman_id = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
c = 0
num = 0
for i in range(len(s) - 1, -1, -1):
j = roman_id[s[i]]
# 如果當(dāng)前字符小于上次迭代的字符鹅巍,那么屬于特殊請求(e.g.,CD,IV..)
if j < c:
num -= j
# 否則直接相加
if j >= c:
num += j
c = j
return num
如果您有更好的實現(xiàn)方法,歡迎交流料祠。
ps:如果您有好的建議骆捧,歡迎交流 :-D,也歡迎訪問我的個人博客:tundrazone.com