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

[leetcode] 13. Roman to Integer

时间:2016-03-22 10:12:51      阅读:148      评论: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 val(char s)
    {
        switch (s) 
        {
            case ‘I‘: return 1;
            case ‘V‘: return 5;
            case ‘X‘: return 10;
            case ‘L‘: return 50;
            case ‘C‘: return 100;
            case ‘D‘: return 500;
            case ‘M‘: return 1000;
        }
    }
    int romanToInt(string s) {
        if(s[0]==NULL) return 0;
        char q=s[0],t=s[1];
        int sum=0,i=1;
        while(t!=NULL)
        {
            if(val(q)>=val(t)) sum=sum+val(q);
            else sum=sum-val(q);
            q=s[i];
            t=s[i+1];
            i++;
        }
        sum=sum+val(q);
        return sum;
    }
};

  

[leetcode] 13. Roman to Integer

标签:

原文地址:http://www.cnblogs.com/yuchenkit/p/5304868.html

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