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

leetcode[13]Roman to Integer

时间:2015-02-10 14:39:57      阅读:111      评论: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 len=s.length();
    if(len<1)return 0;
    map<char,int> maps;
    maps[‘I‘]=1;
    maps[‘V‘]=5;
    maps[‘X‘]=10;
    maps[‘L‘]=50;
    maps[‘C‘]=100;
    maps[‘D‘]=500;
    maps[‘M‘]=1000;
    int i=len-1;
    int res=maps[s[i]];
    while(i>0)
    {
        if(maps[s[i]]<=maps[s[--i]])
        {
            res+=maps[s[i]]; 
        }
        else
        {
            res-=maps[s[i]]; 
        }
    }
    return res;
    }
};

 

leetcode[13]Roman to Integer

标签:

原文地址:http://www.cnblogs.com/Vae98Scilence/p/4283692.html

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