[leetcode] 13. 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 val(char s)
    {
        switch (s) 
        {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
        }
    }
    int romanToInt(string s) {
        if(s[0]==NULL) return 0;
        char q=s[0],t=s[1];
        int sum=0,i=1;
        while(t!=NULL)
        {
            if(val(q)>=val(t)) sum=sum+val(q);
            else sum=sum-val(q);
            q=s[i];
            t=s[i+1];
            i++;
        }
        sum=sum+val(q);
        return sum;
    }
};

  

posted @ 2016-03-22 09:38  白天黑夜每日c  阅读(132)  评论(0编辑  收藏  举报