#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
https://oj.leetcode.com/problems/roman-to-integer/
Roman to Integer
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
===comments by dabay===
First Google a little bit of Roman numerals:
I-1
V-5
X-10
L-50
C-100
D-500
M-1000
The main problem is to consider a number of 4,40 and other representations.
You can go from right to left, and then process it in turn:
This number is subtracted when the number represented by this letter is smaller than the latter;
‘‘‘
Class Solution:
# @return An integer
def romantoint (self, s):
Roman_dict = {"I": 1, "V": 5, "X": Ten, "L": +, "C": +, "D": $, "M": 1000}
If Len (s) = = 0:
return 0
Value = 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 = Value-roman_dict[s[i]]
Else
Value = 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