LeetCode 13 Roman to Integer

来源:互联网 发布:传奇db数据库编辑器 编辑:程序博客网 时间:2024/06/06 12:34

题意:

把罗马数字变成数字。


思路:

简单模拟。我用了个map有点蠢,其实写个switch更好点吧。


代码:

//// Created by house on 1/9/17.//class Solution {public:    int romanToInt(string s) {        map<char, int> toint;        toint['I'] = 1;        toint['V'] = 5;        toint['X'] = 10;        toint['L'] = 50;        toint['C'] = 100;        toint['D'] = 500;        toint['M'] = 1000;        int ans = 0;        for (int i = 0; i < s.size(); ++i) {            int cur = toint[s[i]];            int nex = -1;            if (i + 1 < s.size() && toint[s[i + 1]] > cur) {                cur = -cur;            }            ans += cur;        }        return ans;    }};


0 0
原创粉丝点击