首页 > 其他 > 详细

Roman to Integer

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