Leetcode(5) - Roman numeral to integer

Roman numerals contain the following seven characters: IVXL, C, D and  M.

character value
I             1
V 5
X             10
L             50
C             100
D             500
M             1000

For example, the Roman numeral 2 is written  II as two parallel 1s. 12 is written  XII as  X +  II . 27 is written   XXVIIas  XX +  V +  II .

Typically, smaller numbers in Roman numerals are to the right of larger numbers. But there are special cases, such as 4 is not written  IIII, but  IV. The number 1 is to the left of the number 5, and the number represented is equal to the number 4 obtained by subtracting the number 1 from the large number 5. Likewise, the number 9 is represented as  IX. This particular rule only applies to the following six cases:

  • I Can be placed  to the left of V (5) and  X (10) to represent 4 and 9.
  • X Can be placed  to the left of L (50) and  C (100) to represent 40 and 90. 
  • C Can be placed  to the left of D (500) and  M (1000) to represent 400 and 900.

Given a Roman numeral, convert it to an integer. Make sure the input is in the range of 1 to 3999.

class Solution {
public:
int romanToInt(string s)
{
    if(s.empty()) return 0;
    int n = s.size();
    int out=0;
    for(int i=0;i<n;i++)
    {
        switch(s[i])
        {
        case 'I':
                if(i+1<n && s[i+1]=='V')
                {
                    out+=4;
                    i++;
                }
                else if(i+1<n && s[i+1]=='X')
                {
                    out += 9;
                    i++;
                }
                else
                    out +=1;
                break;
        case 'V':
            out += 5;
            break;
        case 'X':
                if(i+1<n && s[i+1]=='L')
                {
                    out+=40;
                    i++;
                }
                else if(i+1<n && s[i+1]=='C')
                {
                    out += 90;
                    i++;
                }
                else
                    out +=10;
                break;
        case 'L':
            out += 50;
            break;
        case 'C':
                if(i+1<n && s[i+1]=='D')
                {
                    out+=400;
                    i++;
                }
                else if(i+1<n && s[i+1]=='M')
                {
                    out += 900;
                    i++;
                }
                else
                    out +=100;
                break;
        case 'D':
            out += 500;
            break;
        case 'M':
            out +=1000;
            break;
        }
    }
    return out;
}
};

It mainly uses the switch-case structure to judge the corresponding value of each bit in turn, focusing on the judgment of two consecutive cases, such as IV, IX, etc. In addition, pay attention to the problem of out-of-bounds access.

int romanToint(string s) {
        int tagVal[256];  
        tagVal['I'] = 1;  
        tagVal[ ' V ' ] = 5 ;  
        tagVal['X'] = 10;  
        tagVal['C'] = 100;  
        tagVal['M'] = 1000;  
        tagVal['L'] = 50;  
        tagVal['D'] = 500;  
        int val = 0;  
        for(int i = 0; i < s.length(); i++){  
            if(i+1 >= s.length() || tagVal[s[i+1]] <= tagVal[s[i]])  
                val += tagVal[s[i]];  
            else  
                val -= tagVal[s[i]];   
        }  
        return val;   
    }

I borrowed this code from others and abandoned the judgment method of the switch-case structure. He used the enumeration method, and then used numbers such as IX and IV, and the latter one was larger than the previous one to judge, very smart.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324968833&siteId=291194637