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 i=0;
        int sum=0;
        map<char,int> roman={{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
        int length=s.size();
        for(i=0;i<length;i++){
            if(roman[s[i]]<roman[s[i+1]])
                sum-=roman[s[i]];
            else
                sum+=roman[s[i]];
        }
        return sum;
    }
};

 

posted @ 2015-10-16 11:52  michaelzhao10  阅读(123)  评论(0编辑  收藏  举报