[leetcode] 13. Roman to Integer

Posted 白天黑夜每日c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] 13. Roman to Integer相关的知识,希望对你有一定的参考价值。

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

class Solution {
public:
    int val(char s)
    {
        switch (s) 
        {
            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;
        }
    }
    int romanToInt(string s) {
        if(s[0]==NULL) return 0;
        char q=s[0],t=s[1];
        int sum=0,i=1;
        while(t!=NULL)
        {
            if(val(q)>=val(t)) sum=sum+val(q);
            else sum=sum-val(q);
            q=s[i];
            t=s[i+1];
            i++;
        }
        sum=sum+val(q);
        return sum;
    }
};

  

以上是关于[leetcode] 13. Roman to Integer的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 13. Roman to Integer(python)

leetcode-13-Roman to Integer

#Leetcode# 13. Roman to Integer

[leetcode] 13. Roman to Integer

LeetCode 13. Roman to Integer

Leetcode 13. Roman to Integer