This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

background

Learning the front end of three months, ready to brush interview questions, summary summary, a few interview questions a day, to the big factory.

Today we will brush the Roman numeral to integer this problem, this problem is actually very simple, mainly to find the law, the law to find, we can write out the basic easily.

Problem: Convert Roman numerals to whole numbers

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

character The numerical
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Such as:

The Roman numeral 2 is written as II, as two ones side by side. Write XII as X + II. Write XXVII as XX + V + II.

Usually, the smaller Roman numerals are to the right of the larger numerals. But there are exceptions, for example, 4 is not written as IIII, it’s written as IV. The number 1 is to the left of the number 5 and represents the number 4 when the larger number 5 decreases by 1. Similarly, the number 9 represents IX. This particular rule applies only to the following six situations:

I can be put 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 put to the left of D (500) and M (1000) to represent 400 and 900.

Given a Roman numeral, convert it to an integer.

Example 1:

Enter: s ="III"Output:3
Copy the code

Example 2:

Enter: s ="IX"Output:9
Copy the code

parsing

If we look at the problem, normally, one Roman character corresponds to one number. So let’s set up a Map.

Let’s look at the abnormal case: The abnormal case is usually composed of two characters, representing a number. One result of looking at unusual combinations of Roman characters is that the Roman numerals are usually smaller than the numerals and to the right of the larger numerals. The special case is that the Roman numerals are smaller to the left of larger numerals. So why don’t we just compare two characters at a time and say it’s a combination of characters, but a single character.

And we also found: special Roman numerals, you just need to take the small numbers in reverse, negative numbers. So we add up individual characters to get integer numbers.

Code:

    / * * *@param {string} s
 * @return {number}* /
var romanToInt = function(s) {
    var mapObj={
        'I':1.'V':5.'X':10.'L':50.'C':100.'D':500.'M':1000
    };
    / / integer and
    let totalNum = 0;
    const n = s.length;
    for (let i = 0; i < n; ++i) {
        const value = mapObj[s[i]];
        // Take the next number and compare it to see if it is a special number.
        const nextValue=mapObj[s[i + 1]] | |0;
        if (value < nextValue) {
            // Special characters. The value on the left is negative
            totalNum -= value;
        } else{ totalNum += value; }}return totalNum;
};
Copy the code

validation

conclusion

One step at a time, solid work!