首页 > 其他 > 详细

leetcode:Roman to Integer

时间:2015-04-18 17:43:42      阅读:303      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    int romanToInt(string s) {
         int map[128];
        if(s.size()==0)
            return 0;
        map[‘I‘]=1;
        map[‘V‘]=5;
        map[‘X‘]=10;
        map[‘L‘]=50;
        map[‘C‘]=100;
        map[‘D‘]=500;
        map[‘M‘]=1000;
        int res = map[s[0]];
        for(int i=1;i<s.size();i++)
        {
            if(map[s[i]]<=map[s[i-1]])
                res += map[s[i]];
            else
                res = res-2*map[s[i-1]]+map[s[i]];
        }
        return res;
    }
};

leetcode:Roman to Integer

原文:http://blog.csdn.net/majing19921103/article/details/45114759

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!