[Daily leavet] Roman numerals to integer

Roman numerals contain the following seven characters: I, V, X, L, 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 as II, which is the two parallel 1.12 written as XII, which is X + II. 27 Write XXVII, which is XX + V + II. Usually, the small number in the Roman numeral is to the right of the big number. But there are also special cases, such as 4 not written as IIII, but IV. The number 1 is to the left of the number 5, and the number represented is equal to the large number 5 minus the number 1 to get the value 4 . Similarly, the number 9 is represented as IX. This special rule applies only to the following six situations: 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. The input is guaranteed to be in the range of 1 to 3999. Example 1: Enter: "III" Output: 3 Example 2: Enter: "IV" Output: 4 Example 3: Enter: "IX" Output: 9 Example 4: Enter: "LVIII" Output: 58 Explanation: L = 50, V = 5, III = 3. Example 5: Enter: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

: c++

class Solution {
public:
    int romanToInt(string s) {
        int n=0;
        for(int i=s.length()-1;i>=0;i--)
        {
            if(s[i]=='I')
                n=n+1;
            if(s[i]=='V')
            {
                if(s[i-1]=='I')
                    n=n+5-2;
                else
                    n=n+5;
            }
            if(s[i]=='X')
            {
                if(s[i-1]=='I')
                    n=n+10-2;
                else                    
                    n=n+10;
            }
            if(s[i]=='L')
            {
                if(s[i-1]=='X')
                    n=n+50-20;
                else
                    n=n+50;
            }

            if(s[i]=='C')
            {
                if(s[i-1]=='X')
                    n=n+100-20;
                else
                    n=n+100;
            }              
            if(s[i]=='D')
            {
                if(s[i-1]=='C')
                    n=n+500-200;
                else
                    n=n+500;
            }                
            if(s[i]=='M')
            {
                if(s[i-1]=='C')
                    n=n+1000-200;
                else
                    n=n+1000;
            }

        }
        return n;
    }
};