将罗马数字转为阿拉伯数字,用python解
答案:2 悬赏:0 手机版
解决时间 2021-02-26 02:26
- 提问者网友:辞取
- 2021-02-25 11:43
将罗马数字转为阿拉伯数字,用python解
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2021-02-25 12:11
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
romanInt = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000};
num = romanInt[s[0]];
for i in range(1,len(s)):
if romanInt[s[i]] > romanInt[s[i - 1]]:
num += romanInt[s[i]] - 2 * romanInt[s[i - 1]];
else:
num += romanInt[s[i]];
return num;
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
romanInt = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000};
num = romanInt[s[0]];
for i in range(1,len(s)):
if romanInt[s[i]] > romanInt[s[i - 1]]:
num += romanInt[s[i]] - 2 * romanInt[s[i - 1]];
else:
num += romanInt[s[i]];
return num;
全部回答
- 1楼网友:三千妖杀
- 2021-02-25 13:15
你说呢...
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯