Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

class Solution {
public:
    int romanToInt(string s) {
        int val[256];
        val['I']=1;
        val['V']=5;
        val['X']=10;
        val['L']=50;
        val['C']=100;
        val['D']=500;
        val['M']=1000;
        if(s.length()==0)
            return 0;
        int num=val[s[0]];
        for(int i=1;i<s.length();i++)
        {
            if(val[s[i]]<=val[s[i-1]])
                num=num+val[s[i]];
            else
                num=num-val[s[i-1]]-val[s[i-1]]+val[s[i]];
        }
        return num;
    }
}; 
原文地址:https://www.cnblogs.com/erictanghu/p/3759235.html