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

Roman to Integer

时间:2016-01-03 22:22:00      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

 

Subscribe to see which companies asked this question

按照intger to roman 的思路,写下面这道题,比较绕, 而且写的不够简洁,而且修改了好几次

class Solution {
public:
    int romanToInt(string s) {
        string c[4][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},
                         {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
                         {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
                         {"0","M","MM","MMM"}};

        int len = s.length();
        if (0 == len) {
            return 0;
        }
        int begin = 0;
        int i;
        int res = 0;
        while (begin < len) {
            if (s[begin] == M) {
                for (i=3; i>0; i--) {
                    if (s.find(c[3][i]) != string::npos) {
                        res += i * 1000;
                        begin = begin + c[3][i].length();
                        break;
                    }
                }
            } else if (s[begin] == C || s[begin] == D) {
                for (i=9; i>0; i--) {
                    if (s.find(c[2][i]) != string::npos) {
                        //当找到D的时候,并不能够确定是找到CD 还是D
                        //做一下判断
                        if (begin >=0 && s[begin] == C && s[begin + 1] == D) {
                            res += 4 * 100;
                            begin = begin + c[2][4].length();
                            break;
                        } else {
                            res += i * 100;
                            begin = begin + c[2][i].length();
                            break;
                        }
                    }
                }
            } else if (s[begin] == X || s[begin] == L) {
                for (i=9; i>0; i--) {
                    if (s.find(c[1][i]) != string::npos) {
                        if (begin >=0 && s[begin] == X && s[begin + 1] == L) {
                            res += 4 * 10;
                            begin = begin + c[1][4].length();
                            break;
                        } else {
                            res += i * 10;
                            begin = begin + c[1][i].length();
                            break;
                        }
                    }
                }
            } else if (s[begin] == I || s[begin] == V) {
                for (i=9; i>0; i--) {
                    if (s.find(c[0][i]) != string::npos) {
                        if (begin >=0 && s[begin] == I && s[begin + 1] == V) {
                            res += 4 ;
                            begin = begin + c[0][4].length();
                            break;
                        } else {
                            res += i;
                            begin = begin + c[0][i].length();
                            break;
                        }
                    }
                }
            } else {
                return res;
            }
        }
        return res;
    }
};

如果按照下面这种思路写就很简单了

class Solution {
public:
    int romanToInt(string s) {
        map<char,int> roman;
        roman[I]=1;
        roman[V]=5;
        roman[X]=10;
        roman[L]=50;
        roman[C]=100;
        roman[D]=500;
        roman[M]=1000;

        int len = s.length();
        if (0 == len) {
            return 0;
        }

        int res = 0;
        int index;
        for (index=0; index<len; index++) {
            if (index>0 && roman[s[index]] > roman[s[index -1]]) {
                res = res + (roman[s[index]] -2 * roman[s[index -1]]);
            } else {
                res = res + roman[s[index]];
            }
        }

        return res;
    }

};

 

Roman to Integer

标签:

原文地址:http://www.cnblogs.com/SpeakSoftlyLove/p/5097151.html

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