CPP clean linear solution using switch statement


#1
int Solution::romanToInt(string A) {
    int prev = 0, num=0;
    for(int i = A.length()-1; i>= 0; i--)
    {
        switch (A[i])
        {
            case 'I':
                if(prev <= 1)
                    num+= 1;
                else
                    num -= 1;
                prev= 1;
                break;
            case 'V':
                if(prev <= 5)
                    num += 5;
                else
                    num -= 5;
                prev = 5;
                break;
            case 'X':
                if(prev <= 10)
                    num += 10;
                else
                    num -= 10;
                prev = 10;
                break;
            case 'L':
                if(prev <= 50)
                    num += 50;
                else
                    num -= 50;
                prev = 50;
                break;
            case 'C':
                if(prev <= 100)
                    num += 100;
                else
                    num -= 100;
                prev = 100;
                break;
            case 'D':
                if(prev <= 500)
                    num += 500;
                else
                    num -= 500;
                prev = 500;
                break;
            case 'M':
                if(prev <= 1000)
                    num += 1000;
                else
                    num -= 1000;
                prev = 1000;
                break;
            default:
                break;
        }
    }
    return num;
}