Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.
I. Title Description:
- Converting Roman numerals to whole numbers – easy
Roman numerals contain the following seven characters: I, V, X, L, C, D and M.
Character value I1
V 5
X 10
L 50
C 100
D 500
M 1000
Copy the code
For example, the Roman numeral 2 is written as II, which is 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:
Input: s = “III” Output: 3
Input: s = “IV” Output: 4
Input: s = “IX” Output: 9
Description: L = 50, V= 5, III = 3. Example 5:
Input: S = “MCMXCIV” Output: 1994 Interpretation: M = 1000, CM = 900, XC = 90, IV = 4
Tip:
1 <= s.length <= 15
S contains only characters (‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’)
The data guarantees that s is a valid Roman numeral and represents an integer within the range [1, 3999]
All test cases are in accordance with the Roman numerals writing rules, and there will be no cross-position.
Examples like IL and IM don’t fit the question, 49 should be XLIX, 999 should be CMXCIX.
For detailed rules for writing Roman numerals, see Roman numerals – Mathematics.
Ii. Topic and Idea Analysis:
This problem is the reverse of the previous one.
Can we do the exact opposite of converting to integers?
Such as:
Rome: MM+DCCC+LXXX+VIII, translated into numbers: 2888
Rome: MM+DCCC, translated into numbers: 2800
Rome: MM+LXXX, translated into numbers: 2080
So you just split Roman numerals, take the corresponding numerals and add them up
How to make accurate segmentation?
Look look, division is not very simple, since this problem is simple, there should be a very simple method
If you look more closely, Roman numerals are concatenated by an additive concatenation, which means you simply iterate over each letter and add up the value of each letter.
Such as:
Rome: M+M+D+C+C+C+L+X+X+X+V+I+I+I +I, which translates into: 2888
Rome: M+M+D+C+C+C, translated into numbers: 2800
Rome: M+M+L+X+X+X, translated into numbers: 2080
There are six exceptions
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.
[V, X, L, C, D, M]
Because it says:
The data guarantees that s is a valid Roman numeral and represents an integer within the range [1, 3999].
All test cases are in accordance with the Roman numerals writing rules, and there will be no cross-position.
So the correct arrangement is only M to I,
Therefore, in the correct arrangement, these six special rules can be replaced by the general rules, namely:
IV is replaced by IIII
IX is replaced by VIIII
.
Or you don’t have to do the substitution, you can just walk through and take the corresponding number, which I think is much less code.
Iii. Code:
The code implementation is as follows:
/ * * *@param {string} s
* @return {number}* /
var romanToInt = function(s) {
let obj = {
I: 1.IV: 4.V: 5.IX: 9.X: 10.XL: 40.L: 50.XC: 90.C: 100.CD: 400.D: 500.CM: 900.M: 1000,}let num = 0
for(let i = 0; i<s.length; i++){
if(['IV'.'IX'.'XL'.'XC'.'CD'.'CM'].includes(s[i]+s[i+1])){
num += obj[s[i]+s[i+1]]
i++
}else{
num += obj[s[i]]
}
}
return num
}
Copy the code
Iv. Summary:
Although the difficulty of this problem is simple, but it is not so simple, MY thinking changed three times, the latter two are right, the first time is really think too complicated.
Come on!