Roman to Integer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 romanToInt(string s) {
        int map[256];
        map[I]=1;map[V]=5;map[X]=10;map[L]=50;map[C]=100;map[D]=500;map[M]=1000;
        int i=-1,len=s.length(),ans=0;
        while(++i<len){
            if(i+1<len&&map[s[i]]<map[s[i+1]]){
                ans-=map[s[i]];
            }
            else
                ans+=map[s[i]];
        }
        return ans;
    }
};

 

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

Leetcode:Integer to Roman

Integer to Roman/Roman to Integer

Integer to Roman LeetCode Java

Leetcode 12——Integer to Roman

LeetCode(12)Integer to Roman

LeetCode Medium:12. Integer to Roman