Roman to Integer

Jul 31, 2016



#!/usr/bin/env python
# coding=utf-8
def roman2int(s):
    ret = 0
    index = len(s) - 1
    tmp = 0
    dt={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
    while index >= 0:
        v = dt[s[index]]
        if tmp == 0:
            tmp = v
        else:
            if v < tmp:
                tmp -= v
            else:
                ret += tmp
                tmp = v
        index -= 1
    ret += tmp
    return ret