Leetcode-Roman to Integer-Python
2017-08-14 11:44
309 查看
Roman to Integer
将罗马数字转换为阿拉伯数字。Description
leetcode没有说明具体的转换规则,我是按照罗马数字规则设置的。
解题思路:
一个保存罗马数字映射的字典:
roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}
规则:
- 如果一个元素值比右边元素大,则加上这个元素;
- 如果一个元素值比右边元素小,则减去这个元素;
- 始终加上末尾的元素;
def romanToInt(self, s): """ :type s: str :rtype: int """ roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1} num = 0 for i in range(len(s)-1): if roman[s[i] 9c29 ]>=roman[s[i+1]]: num = num + roman[s[i]] else: num = num - roman[s[i]] return num+roman[s[-1]]
相关文章推荐
- Leetcode 12. Integer to Roman(python)
- [leetcode]Roman to Integer @ Python
- [leetcode: Python]12. Integer to Roman
- 12. Integer to Roman Leetcode Python
- 13. Roman to Integer Leetcode Python
- 【LeetCode with Python】 Roman to Integer
- LeetCode 12 Integer to Roman(C,C++,Java,Python)
- leetcode Roman to Integer python
- Leetcode 12. Integer to Roman The Solution of Python
- [LeetCode]题解(python):012-Integer to Roman
- [leetcode]Integer to Roman @ Python
- LeetCode 13 Roman to Integer (C,C++,Java,Python)
- leetcode(10),Roman to Integer(python)
- leetcode Integer to Roman python
- Leetcode 13. Roman to Integer The Solution of Python
- [leetcode] Roman to Integer @ Python
- python写算法题:leetcode: 12. Integer to Roman
- 【LeetCode with Python】 Integer to Roman
- [leetcode] Integer to Roman @ Python
- 【Leetcode】【python】Integer to Roman