给定一个罗马数字,将其转换成整数。

返回的结果要求在 1 到 3999 的范围内。

详见:https://leetcode.com/problems/roman-to-integer/description/

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

 参考:http://www.cnblogs.com/grandyang/p/4120857.html

相关文章:

  • 2022-02-21
  • 2022-01-12
  • 2021-11-18
  • 2021-09-21
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
猜你喜欢
  • 2021-11-23
  • 2021-12-05
  • 2022-12-23
  • 2021-05-26
  • 2021-07-08
  • 2021-06-17
  • 2022-01-28
相关资源
相似解决方案