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

Roman to Integer

时间:2015-05-11 21:55:41      阅读:133      评论: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) {
    	unordered_map<string,int> mp = {{"M",1000},{"CM",900},{"D",500},
    			{"CD",400},{"C",100},{"XC",90},{"L",50},{"XL",40},{"X",10},
				{"IX",9},{"V",5},{"IV",4},{"I",1}};
    	int res = 0;
    	for(int i=0;i<s.length();i++){
    		if(i+2<=s.length()+1){
    			auto iter = mp.find(s.substr(i,2));
    			if(iter!=mp.end()){
    				res += iter->second;
    				i++;
    				continue;
    			}
    		}
    		auto iter = mp.find(s.substr(i,1));
    		res += iter->second;
    	}
    	return res;

    }
};


Roman to Integer

标签:

原文地址:http://blog.csdn.net/guorudi/article/details/45648825

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