Leetcode-13. Roman numeral to integer (C++)

Topic link

https://leetcode-cn.com/problems/roman-to-integer

Title description

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, the Roman numeral 2 is written as II, which means two parallel ones. 12 is written as XII, which means X + II. 27 is written as XXVII, which is XX + V + II.

Normally, the small numbers in Roman numerals are to the right of the large numbers. But there are special cases. For example, 4 is not written as IIII, but as IV. The number 1 is to the left of the number 5, and the number represented is equal to the number 4 obtained by subtracting the number 1 from the large number 5. Similarly, the number 9 is represented as IX. This special rule only applies to the following six situations:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.
X can be placed to the left of L (50) and C (100) to represent 40 and 90. 
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Given a Roman numeral, convert it to an integer. Ensure that the input is in the range of 1 to 3999.

input Output

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V = 5, III = 3.
Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

Problem solving ideas

The value is simply to judge the next digit of the next character. If it can form a number with the next digit, then the group will be grouped. If not, the value will be calculated directly.

Problem-solving code

#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
    int getTwoNum(char num1, char num2) {
//        I可以放在V(5) 和 X (10) 的左边,来表示 4 和 9。
//        X可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
//        C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。

        if (num1=='I' && (num2 =='V' || num2=='X')){
            if (num2 =='V'){
                return 4;
            } else{
                return 9;
            }
        }
        if (num1=='X' && (num2 =='L' || num2=='C')){
            if (num2 =='L'){
                return 40;
            } else{
                return 90;
            }
        }
        if (num1=='C' && (num2 =='D' || num2=='M')){
            if (num2 =='D'){
                return 400;
            } else{
                return 900;
            }
        }
        else{
            return -1;
        }
    }

    int romanToInt(string s) {

        int index = 0;
        int sum = 0;
        while (index < s.length()) {
            char romeNum1 = s[index];
            if (index == s.length()) {
                sum += getNum(romeNum1);
            } else {
                char romeNum2 = s[index + 1];
                int twosum = getTwoNum(romeNum1, romeNum2);
                if (twosum > 0) {
                    sum += twosum;
                    index++;
                } else {
                    sum += getNum(romeNum1);
                }
            }
            index++;
        }
        return sum;

    }

private:
    int getNum(char roma) {
        switch (roma) {
            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;
            default:
                return 0;

        }
    }
};



//int main() {
//    Solution solution;
//    int k = solution.romanToInt("L");
//    cout<<k;
//    return 0;
//}


 

Guess you like

Origin blog.csdn.net/Kangyucheng/article/details/108476773