Convert Roman numerals to integers

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

I 1 V 5 X 10 L 50 C 100 D 500 M 1000 The Roman numeral 2 is written II. Let’s write 12 as XII, which is X plus II. 27 write XXVII, XX + V + II.

In Roman numerals, the smaller number is usually placed to the right of the larger number. But there are exceptions. For example, instead of writing IIII for 4, I would write IV. The number 1 is to the left of the number 5, which is equal to the large number 5 and the number 4 is obtained by reducing the number 1. Similarly, the number 9 is represented as IX. This particular rule only applies in the following six cases:

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. Make sure the input is in the range 1 to 3999.

Train of thought

  • The main point of this question is to add the values corresponding to each Roman numeral to get the final output, but there are special Roman numeral, such as IV and IX, two Roman numeral for one value, so special treatment.
  • For special handling, there are two methods:
  1. Looking backwards, if the value corresponding to the current Roman numeral is less than the value corresponding to the Roman numeral to its right, subtract the value corresponding to the current Roman numeral, otherwise add the value corresponding to the current Roman numeral.
  2. Looking backwards, if the value corresponding to the current Roman numeral is less than the value corresponding to the Roman numeral to its right, subtract the value corresponding to the current Roman numeral, otherwise add the value corresponding to the current Roman numeral.

Method a

func romanToInt(s string) int {
    hashMap:=map[byte]int{
        'I':1.'V':5.'X':10.'L':50.'C':100.'D':500.'M':1000,}// Define and initialize the map, specifying the relationship between Roman numerals and their corresponding values
    sum:=0// Define the output result variable
    for i:=0; i<len(s); i++{ cur:=hashMap[s[i]]if i<len(s)- 1&&cur<hashMap[s[i+1]]{
            sum=sum-cur
        }else{
            sum=sum+cur
        }
    }// From the front, if the value of the current Roman numeral is less than the value of the Roman numeral to its right, subtract the value of the current Roman numeral, otherwise add the value of the current Roman numeral.
    return sum
}

Copy the code

Method 2

func romanToInt(s string) int {
    hashMap:=map[byte]int{
        'I':1.'V':5.'X':10.'L':50.'C':100.'D':500.'M':1000,}// Define and initialize the map, specifying the relationship between Roman numerals and their corresponding values
    sum:=0// Define the output result variable
    pre:=0// Define to store the variables on the right
    for i:=len(s)- 1; i>=0; i--{ cur:=hashMap[s[i]]if cur>=pre{
            sum+=cur
        }else{
            sum-=cur
        }
        pre=cur
    }// If the value of the current Roman numeral is less than the value of the Roman numeral to the right, subtract the value of the current Roman numeral, otherwise add the value of the current Roman numeral.
    return sum
}
Copy the code