Algorithm exercise: Roman numeral to integer

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

Character           numerical 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  , That is, two parallel ones. 12 write do   XII  , mean   X  +  II  . 27 write do    27. Namely   XX  +  V  +  II  .

Usually, the small Roman numerals are to the right of the large ones. However, there are also special cases, such as 4 do not write   IIII, but   IV. The number 1 is on the left of the number 5, and the number represented is equal to the value 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is expressed as   IX. This special rule applies only to the following six cases:

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

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


1 <= s.length <= 15
s contains only characters ('I ',' V ',' x ',' l ',' C ','d','m ')
The title data ensures that s is a valid Roman numeral and represents an integer in the range [1, 3999]
The test cases given in the title comply with the Roman numeral writing rules, and there will be no cross position and so on.
Examples such as IL and IM do not meet the title requirements. 49 should write XLIX and 999 should write CMXCIX.
For detailed writing rules of Roman numerals, please refer to Roman numerals - Mathematics.

Source: LeetCode
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.


According to the description of the topic, the following rules can be summarized:

Roman numerals are composed of I, V, x, l, C, D and m;
When the small value is to the left of the large value, reduce the value, such as IV=5-1=4;
When the small value is on the right of the large value, add the small value, such as VI=5+1=6;
It can be seen from the above that the right value is always positive, so the last digit must be positive.
In short, putting a small value to the left of a large value is subtraction, otherwise it is addition

You can also keep the value of the current bit. When traversing to the next bit, compare the size relationship between the reserved value and the traversal bit, and then determine whether the reserved value is plus or minus. The last one can add.


class Solution {
    public int romanToInt(String s) {

        int sum=0;
       int preNum = getValue(s.charAt(0));
       for(int i=1;i<s.length();i++){
           int num = getValue(s.charAt(i));
               sum -= preNum;
               sum += preNum;
           preNum = num;
       sum +=preNum;

       return sum;

    public int  getValue(char ch){
            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;


Code 2:

class Solution {
    public int romanToInt(String s) {
        s = s.replace("IV","a");
        s = s.replace("IX","b");
        s = s.replace("XL","c");
        s = s.replace("XC","d");
        s = s.replace("CD","e");
        s = s.replace("CM","f");
        int result = 0;
        for (int i=0; i<s.length(); i++) {
            result += which(s.charAt(i));
        return result;

    public int which(char ch) {
        switch(ch) {
            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;
            case 'a': return 4;
            case 'b': return 9;
            case 'c': return 40;
            case 'd': return 90;
            case 'e': return 400;
            case 'f': return 900;
        return 0;

Tags: Algorithm leetcode Machine Learning AI

Posted on Sun, 10 Oct 2021 21:51:39 -0400 by FURQAN