46. leetCode 13: Roman to Integer【罗马数转化为数字】
2018-02-06 15:28
288 查看
【题目】:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
【参考】:参考资料
【Python代码】:
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman_map = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
length = len(s)
result = roman_map[s[0]]
i = 1
while i < length:
if roman_map[s[i - 1]] < roman_map[s[i]]:
result = result + roman_map[s[i]] - 2*roman_map[s[i-1]]
else:
result = result + roman_map[s[i]]
i += 1
return result
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
【参考】:参考资料
【Python代码】:
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman_map = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
length = len(s)
result = roman_map[s[0]]
i = 1
while i < length:
if roman_map[s[i - 1]] < roman_map[s[i]]:
result = result + roman_map[s[i]] - 2*roman_map[s[i-1]]
else:
result = result + roman_map[s[i]]
i += 1
return result
相关文章推荐
- [LeetCoce 13] Roman to Integer 罗马数字转化为数字
- leetcode:Integer to Roman(整数转化为罗马数字)
- [LeetCode]12. Integer to Roman(整数转化为罗马数字)
- Roman to Integer 罗马数字转化成整数
- Leet Code 13 Roman to Integer - 罗马数字转成整数 - Java
- Leetcode 13 Roman to Integer 罗马数字转整数
- [LeetCode] Integer to Roman 整数转化成罗马数字
- Roman to Integer 罗马数字转化成整数
- LeetCode--Roman to Integer 罗马数字转化成整数
- [LeetCode] Roman to Integer 罗马数字转化成整数
- [LintCode] Roman to Integer 罗马数字转化成整数
- Lettcode_13_Roman to Integer——罗马数字转变算法
- 45. leetCode12: Integer to Roman【数字转化为罗马字母】
- Roman to Integer 罗马数字转化为整数
- LeetCode OJ 之 Integer to Roman (整数转化为罗马数字)
- LeetCode Roman to Integer 罗马数字转阿拉伯数字
- [LeetCode] Roman to Integer 罗马数字转整数
- leetcode 12 Integer to Roman(整型数转换为罗马数字)
- (LeetCode) Roman to Integer --- 罗马数字转整数
- leetcode 12 ,13 Integer to Roman &&Roman to Integer 罗马与阿拉伯数组转换