Leetcode 13 Roman to Integer

来源:互联网 发布:js获取div的内容 编辑:程序博客网 时间:2024/06/05 11:46

Given a roman numeral, convert it to an integer.

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

和12正好相反,理解转换规则,打一个表就行了

class Solution {public:    int romanToInt(string s) {        map<char,int> mp;        mp['I']=1;        mp['V']=5;        mp['X']=10;        mp['L']=50;        mp['C']=100;        mp['D']=500;        mp['M']=1000;        int result=0,pre=0;        for(int i=0;i<s.length();i++)        {            if(mp[s[i]]>pre)                result+=mp[s[i]]-2*pre;            else                result+=mp[s[i]];            pre=mp[s[i]];        }        return result;    }};



0 0
原创粉丝点击