LeetCode | 13. Roman to Integer

Description

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Solution

How does Roman numeric system work?

There are seven basic symbols and the corresponding value of each is following:

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

Usually, symbols are placed from left to right in order of value, starting with the largest, and we add the value of each symbol to calculate the value of the number.

MLXVI : 1000+50+10+5+1=1066

However, there are also some rules to form numbers:

  • When I, X or C placed before symbol values bigger than itself, it becomes subtrahend

IX: 10-1=9

CML:1000-100+50=950

  • As a minuend, a symbol can only subtract the nearest subtrahend in order of value;

Wrong : IC=100-1=99

Correct : XCIX=(100-10)+(10-1)=99

  • There are only one subtrahend before one minuend;

Wrong : IIX=10-1-1=8

Correct : VIII=5+1+1+1=8

  • The same symbol can't repeat four times or more continuously.

Wrong : IIII=1+1+1+1=4

Correct : IV=5-1=4

Thought

  • Read from right to left;
  • Consider possibility of the next symbol to be subtrahend;
minuend subtrahend
I V
X L
C D,M

Code

//JAVA
class Solution { 
public int romanToInt(String s) {
        int len = s.length();
        int result = 0;
        for(int i =len-1;i>=0;i--){                      
            switch(s.charAt(i)){
                case 'I':
                    result+=1;
                    break;
                case 'V':
                    if(i>=1 && s.charAt(i-1)=='I'){
                        result+=5-1;
                        i--;
                    }
                    else{
                    result+=5;
                    }
                    break;
                case 'X':
                    if(i>=1 && s.charAt(i-1)=='I'){
                        result+=10-1;
                        i--;
                    }
                    else{
                    result+=10;
                    }
                    break;
                case 'L':
                    if(i>=1 && s.charAt(i-1)=='X'){
                        result+=50-10;
                        i--;
                    }
                    else{
                    result+=50;
                    }
                    break;
                case 'C':
                    if(i>=1 && s.charAt(i-1)=='X'){
                        result+=100-10;
                        i--;
                    }
                    else{
                    result+=100;
                    }
                    break;
                case 'D':
                    if(i>=1 && s.charAt(i-1)=='C'){
                        result+=500-100;
                        i--;
                    }
                    else{
                    result+=500;
                    }
                    break;
                case 'M':
                    if(i>=1 && s.charAt(i-1)=='C'){
                        result+=1000-100;
                        i--;
                    }
                    else{
                        result+=1000;
                    }
                    break;
                default:
                    System.out.println("defult");
                    break;    
            }
        }
    return result;   
    }
}
Comments
Write a Comment