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

Roman to Integer

时间:2015-10-16 13:35:14      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

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

 

Roman to Integer

标签:

原文地址:http://www.cnblogs.com/michaelzhao10/p/4884907.html

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