首页 > 其他 > 详细

LeetCode: Roman to Integer

时间:2016-03-18 17:40:21      阅读:135      评论:0      收藏:0      [点我收藏+]

13. Roman to Integer

 
Total Accepted: 77116 Total Submissions: 199727 Difficulty: Easy

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 map[26];
        char romanChar[] = { ‘I‘, ‘V‘, ‘X‘, ‘L‘, ‘C‘, ‘D‘, ‘M‘ };
        map[‘I‘ - ‘A‘] = 1;
        map[‘V‘ - ‘A‘] = 5;
        map[‘X‘ - ‘A‘] = 10;
        map[‘L‘ - ‘A‘] = 50;
        map[‘C‘ - ‘A‘] = 100;
        map[‘D‘ - ‘A‘] = 500;
        map[‘M‘ - ‘A‘] = 1000;
        int res = 0, tmp = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (map[s[i] - ‘A‘] > tmp)
            {
                res = res + map[s[i] - ‘A‘] - tmp * 2;
                
            }
            else
            {
                res = res + map[s[i] - ‘A‘];
            }
            tmp = map[s[i] - ‘A‘];
        }
        return res;
    }
};

 

LeetCode: Roman to Integer

原文:http://www.cnblogs.com/luchenxu/p/5292737.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!