Roman to Integer

Given a roman numeral, convert it to an integer.

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

 

Code:

class Solution {
public:
    int romanToInt(string s) {
        int len=s.length();
        map<char,int> character;
        character['I'] = 1;
        character['V'] = 5;
        character['X'] = 10;
        character['L'] = 50;
        character['C'] = 100;
        character['D'] = 500;
        character['M'] = 1000;
        int sum=character[s[len-1]];
        for(int i=0;i<len-1;i++){
            if(character[s[i]]>=character[s[i+1]])
                sum+=character[s[i]];
            else
                sum-=character[s[i]];
        }
        return sum;
    }
};

 

posted @ 2013-11-08 17:39  WinsCoder  阅读(105)  评论(0编辑  收藏  举报