首页 > 其他 > 详细

LeetCode "Roman to Integer"

时间:2015-06-30 06:34:19      阅读:170      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!