2018-09-08 leetcode Permutations Roman to IntegerGreat Version1234567891011class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ res, p = 0, 'I' DICT = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} for x in s[::-1]: res, p = res - DICT[x] if DICT[x] < DICT[p] else res + DICT[x], x return res Ugly Version12345678910111213141516171819202122232425262728class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ res = [] LEFT = {'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900} DICT = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} list_s = list(s) len_s = len(s) i = 0 while True: if i == len_s - 1: res.append(DICT[list_s[i]]) break elif i + 1 < len_s: left = list_s[i] + list_s[i + 1] if left in LEFT: res.append(LEFT[left]) i += 2 else: res.append(DICT[list_s[i]]) i += 1 else: break # print res return sum(res) output 12In [476]: s = Solution(); print s.romanToInt('MCDLXXVI')1476 leet code Newer Best Time to Buy and Sell Stock II Older Permutations