码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Roman to Integer"

时间:2015-06-30 06:34:48      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
    int romanToInt(string s) 
    {
        std::unordered_map<char, int> hm;
        hm[M] = 1000;
        hm[D] = 500;
        hm[C] = 100;
        hm[L] = 50;
        hm[X] = 10;
        hm[V] = 5;
        hm[I] = 1;
                
        int i = 0, len = s.length(), ret = 0;
        while(i < len)
        {
            char c = s[i];
            if(i < len - 1 && hm[s[i + 1]] > hm[s[i]])
            {
                ret += hm[s[i + 1]] - hm[s[i]];
                i ++;
            }
            else
            {
                ret += hm[s[i]];
            }
            i ++;
        }
        return ret;
    }
};

LeetCode "Roman to Integer"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4609311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!