My pure c code (AC)


  • 0
    H
    int getNum(char a){
        switch(a){
            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;
            default:
                return 0;
        }
    }
    
    int romanToInt(char* s) {
        int len = strlen(s);
        int current;
        int last = 0;
        int num = 0;
        for(int i = 0; i < len ;i++){
            current = getNum(s[i]);
            if(current > last){
                num = num + current - 2*last;
                last = current;
            }
            else{
                num = num + current;
                last = current;
            }
        }
        return num;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.