zoukankan      html  css  js  c++  java
  • Roman to Integer

    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;
        }
    
    };
  • 相关阅读:
    snmp实用篇
    使用snmp4j实现Snmp功能(三)
    使用snmp4j实现Snmp功能(二)
    使用snmp4j实现Snmp功能(一)
    “百度杯”CTF比赛 九月场_Test(海洋cms前台getshell)
    “百度杯”CTF比赛 九月场_123(文件备份,爆破,上传)
    “百度杯”CTF比赛 九月场_SQLi
    “百度杯”CTF比赛 九月场_SQL
    “百度杯”CTF比赛 九月场_再见CMS(齐博cms)
    “百度杯”CTF比赛 九月场_YeserCMS
  • 原文地址:https://www.cnblogs.com/SpeakSoftlyLove/p/5097151.html
Copyright © 2011-2022 走看看