题目:
https://leetcode.com/problems/roman-to-integer/
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
规则神马的忽略···
耗时:170ms
class Solution:
# @param {string} s
# @return {integer}
def romanToInt(self, s):
i = 0
Int = 0
Len = len(s)
while (i < Len):
#
while (i<Len and s[i]=='M'):
Int += 1000
i += 1
if (i<Len and s[i]=='D'):
Int += 500
i += 1
while (i<Len and s[i]=='C'):
Int += 100
i += 1
elif (s[i:i+2] == 'CM'):
Int += 900
i += 2
elif (s[i:i+2] == 'CD'):
Int += 400
i += 2
else:
while (i<Len and s[i]=='C'):
Int += 100
i += 1
if (i<Len and s[i]=='L'):
Int += 50
i += 1
while (i<Len and s[i]=='X'):
Int += 10
i += 1
elif (s[i:i+2] == 'XC'):
Int += 90
i += 2
elif (s[i:i+2] == 'XL'):
Int += 40
i += 2
else:
while (i<Len and s[i]=='X'):
Int += 10
i += 1
if (i<Len and s[i]=='V'):
Int += 5
i += 1
while (i<Len and s[i]=='I'):
Int += 1
i += 1
elif (s[i:i+2] == 'IX'):
Int += 9
i += 2
elif (s[i:i+2] == 'IV'):
Int += 4
i += 2
else:
while (i<Len and s[i]=='I'):
Int += 1
i += 1
return Int