Short accepted cpp solution


  • 1
    T
    int romanToInt(string s) {
            const int N = s.length();
            int nums[128];
            nums['I'] = 1;
            nums['V'] = 5;
            nums['X'] = 10;
            nums['L'] = 50;
            nums['C'] = 100;
            nums['D'] = 500;
            nums['M'] = 1000;
            int result = 0;
            for(int i = 0; i < N; ++i)
            {
                if(i + 1 < N && nums[s[i+1]] > nums[s[i]])
                    result -=  nums[s[i]]; 
                else
                    result += nums[s[i]];
            }
            return result;
        }

Log in to reply
 

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