12ms C solution


  • 0
    A
    int romanToInt(char* s) {
        int sum = 0;
        int i = 0;
        while (s[i])
        {
            switch (s[i]-'A')
            {
                case 12: sum += 1000; break;
                case 3 : sum += 500; break;
                case 2 : sum += 100; break;
                case 11: sum += 50; break;
                case 23: sum += 10; break;
                case 21: sum += 5; break;
                case 8 : sum += 1; break;
            }
            i++;
        }
        if (strstr(s,"CM") || strstr(s,"CD")) sum-=200;
        if (strstr(s,"XL") || strstr(s,"XC")) sum-=20;
        if (strstr(s,"IX") || strstr(s,"IV")) sum-=2;
        return sum;
    }
    

Log in to reply
 

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