Brute Force C lang solution


  • 0
    B
    inline static int letterToInt(char let) {
        if      (let == 'I')
            return 1;
        else if (let == 'V')
            return 5;
        else if (let == 'X')
            return 10;
        else if (let == 'L')
            return 50;
        else if (let == 'C')
            return 100;
        else if (let == 'D')
            return 500;
        else if (let == 'M')
            return 1000;
        else
            return 0;
    }
    
    // O(n)
    int romanToInt(char* s) {
        int res = 0;
        int onePassCheck = 0;
        int arr[(int)strlen(s)];       
        for(int i = 0; i < strlen(s); i++) {
            arr[i] = letterToInt(s[i]);
            if(onePassCheck && arr[i] > arr[i-1]) {
                arr[i] -= arr[i-1];
                arr[i-1] = 0;
                onePassCheck--;
            }
            else if(!onePassCheck)
                onePassCheck = 1;
            res += arr[i];
        }
        return res;
    }
    

Log in to reply
 

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