LeetCode 13. 罗马数字转整数

题目描述

LeetCode 13. 罗马数字转整数_第1张图片

题解

暴力判断

class Solution {
public:
    int romanToInt(string s) {
        int result = 0;
        for(int i = 0; i < s.length(); i++) {
            if(s.compare(i, 1, "I") == 0 && s.compare(i + 1, 1, "V") == 0) {
                result += 4;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "I") == 0 && s.compare(i + 1, 1, "X") == 0) {
                result += 9;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "I") == 0) {
                result += 1;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "V") == 0) {
                result += 5;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "X") == 0 && s.compare(i + 1, 1, "L") == 0) {
                result += 40;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "X") == 0 && s.compare(i + 1, 1, "C") == 0) {
                result += 90;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "X") == 0) {
                result += 10;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "L") == 0) {
                result += 50;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "C") == 0 && s.compare(i + 1, 1, "D") == 0) {
                result += 400;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "C") == 0 && s.compare(i + 1, 1, "M") == 0) {
                result += 900;
                i++;
                cout << result << "  " << i << endl;
                continue;
            } else if(s.compare(i, 1, "C") == 0) {
                result += 100;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "D") == 0) {
                result += 500;
                cout << result << "  " << i << endl;
                continue;
            }
            
            if(s.compare(i, 1, "M") == 0) {
                result += 1000;
                cout << result << "  " << i << endl;
                continue;
            }
           
        }
        return result;
    }
};

哈希表法

class Solution {
public:
    int romanToInt(string s) {
        unordered_map m {
           {'I', 1},
           {'V', 5},
           {'X', 10},
           {'L', 50},
           {'C', 100},
           {'D', 500},
           {'M', 1000}
        };
        int res = 0;
        for(int i = 0; i < s.size(); i++) {
            if(i+1 < s.size() && m[s[i+1]] > m[s[i]]) {
                res += (m[s[i+1]] - m[s[i]]);
                i++;
            }
            else
                res += m[s[i]];
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode 13. 罗马数字转整数)