首页 > 代码库 > [LeetCode][Python]Roman to Integer

[LeetCode][Python]Roman to Integer

# -*- coding: utf8 -*-
‘‘‘
__author__ = ‘dabay.wang@gmail.com‘
https://oj.leetcode.com/problems/roman-to-integer/
Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.
===Comments by Dabay===
先google一下罗马数字的表示:
I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000
主要问题是考虑一些4,40之类的表示。

可以从右往左,依次处理:
当遇到这个字母表示的数字比后一个小的时候,减去这个数;否则,累加。
‘‘‘

class Solution:
# @return an integer
def romanToInt(self, s):
roman_dict = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
if len(s) == 0:
return 0
value = http://www.mamicode.com/0
for i in xrange(len(s)-1, -1, -1):
if i < len(s)-1 and roman_dict[s[i]] < roman_dict[s[i+1]]:
value = http://www.mamicode.com/value - roman_dict[s[i]]
else:
value = http://www.mamicode.com/value + roman_dict[s[i]]
return value


def main():
s = Solution()
print s.romanToInt("MCMXCIX")


if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)


[LeetCode][Python]Roman to Integer