“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

[B] [C] [D]

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

Character values I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy 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:

  • ICan be placed inVAnd (5)XTo the left of PI (10) to represent 4 and 9.
  • XCan be placed inLAnd (50)CTo the left of 100, to represent 40 and 90.
  • CCan be placed inDAnd (500)MTo the left of 1000, to represent 400 and 900.

Given a Roman numeral, convert it to an integer. Make sure the input is in the range of 1 to 3999.

Example 1:

Input: "III" Output: 3Copy the code

Example 2:

Input: "IV" Output: 4Copy the code

Example 3:

Input: "IX" Output: 9Copy the code

Example 4:

Input: "LVIII" Output: 58 Interpretation: L = 50, V= 5, III = 3Copy the code

Example 5:

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

Tip:

  • 1 <= s.length <= 15
  • sOnly contain characters('I', 'V', 'X', 'L', 'C', 'D', 'M')
  • Subject data guaranteesIs a valid Roman numeral and indicates that the integer is in 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.

1. Define a result variable ret and initialize it to 0 2. Each character of a given string is iterated from left to right, adding the corresponding value to RET 3 based on the content. Return ret

Here’s a look at what characters and character combinations are involved in iterating through a string

M  1000
CM 900
D  500
CD 400
C  100
XC 90
L  50
XL 40
X  10
IX 9
V  5
IV 4
I  1
Copy the code

Above we list all the Roman numerals and the corresponding values of the combination. Then we judge the value to be added according to the current character in the process of traversing the whole string. Finally we return ret. The code is as follows:

var romanToInt = function(s) { let ret = 0; for(let i = 0; i<s.length; i++){ switch(s[i]){ case 'M': ret += 1000 break; case 'C': if(s[i+1]&&s[i+1]==='M'){ ret+=900,i++ }else if(s[i+1]&&s[i+1]==='D'){ ret+=400,i++ }else{ ret+=100 } break; case 'D': ret+=500 break; case 'X': if(s[i+1]&&s[i+1]==='C'){ ret+=90,i++ }else if(s[i+1]&&s[i+1]==='L'){ ret+=40,i++ }else{ ret+=10 } break; case 'L': ret+=50 break; case 'V': ret+=5 break; case 'I': if(s[i+1]&&s[i+1]==='X'){ ret+=9,i++ }else if(s[i+1]&&s[i+1]==='V'){ ret+=4,i++ }else{ ret+=1 } break; } } return ret; };Copy the code

If you have any questions or suggestions, please leave a comment!