【LeetCode】13. Roman to Integer (2 solutions)

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 n = 0;
        char lastC = 0;
        for(int i = 0; i < s.size(); i ++)
        {
            switch(s[i])
            {
                case 'I':
                    n += 1;     
                    lastC = s[i];
                    break;
                case 'V':
                    if(lastC == 'I')
                    {//IV
                        n -= 1;
                        n += 4;
                        lastC = 0;
                    }
                    else
                    {
                        n += 5;     
                        lastC = s[i];   
                    }
                    break;
                case 'X':
                    if(lastC == 'I')
                    {//IX
                        n -= 1;
                        n += 9;
                        lastC = 0;
                    }
                    else
                    {
                        n += 10;  
                        lastC = s[i];
                    }
                    break;
                case 'L':
                    if(lastC == 'X')
                    {//XL
                        n -= 10;
                        n += 40;
                        lastC = 0;
                    }
                    else
                    {
                        n += 50;  
                        lastC = s[i];
                    }
                    break;
                case 'C':
                    if(lastC == 'X')
                    {//XC
                        n -= 10;
                        n += 90;
                        lastC = 0;
                    }
                    else
                    {
                        n += 100;   
                        lastC = s[i];   
                    }
                    break;
                case 'D':
                    if(lastC == 'C')
                    {//CD
                        n -= 100;
                        n += 400;
                        lastC = 0;
                    }
                    else
                    {
                        n += 500;
                        lastC = s[i];
                    }
                    break;
                case 'M':
                    if(lastC == 'C')
                    {//CM
                        n -= 100;
                        n += 900;
                        lastC = 0;
                    }
                    else
                    {
                        n += 1000;
                        lastC = s[i];
                    }
                    break;
                default:
                    return 0;
            }
        }
        return n;
    }
};

 

解法二:递归

处理开头字符情况之后递归处理后续字符串

class Solution {
public:
    int romanToInt(string s) {
        if(s == "")
            return 0;
        //to here, s.size() >= 1
        else if(s[0] == 'M')
            return 1000 + romanToInt(s.substr(1));
        else if(s[0] == 'C')
        {
            if(s.size() == 1)
            //s == "C"
                return 100;
            if(s[1] == 'M')
                return 900 + romanToInt(s.substr(2));
            else if(s[1] == 'D')
                return 400 + romanToInt(s.substr(2));
            else
                return 100 + romanToInt(s.substr(1));
        }
        else if(s[0] == 'D')
            return 500 + romanToInt(s.substr(1));
        else if(s[0] == 'X')
        {
            if(s.size() == 1)
            //s == "X"
                return 10;
            if(s[1] == 'C')
                return 90 + romanToInt(s.substr(2));
            else if(s[1] == 'L')
                return 40 + romanToInt(s.substr(2));
            else
                return 10 + romanToInt(s.substr(1));
        }
        else if(s[0] == 'L')
            return 50 + romanToInt(s.substr(1));
        else if(s[0] == 'I')
        {
            if(s.size() == 1)
            //s == "I"
                return 1;
            if(s[1] == 'X')
                return 9 + romanToInt(s.substr(2));
            else if(s[1] == 'V')
                return 4 + romanToInt(s.substr(2));
            else
                return 1 + romanToInt(s.substr(1));
        }
        else if(s[0] == 'V')
            return 5 + romanToInt(s.substr(1));
    }
};

posted @ 2014-12-21 12:57  陆草纯  阅读(318)  评论(0编辑  收藏  举报