Header Ad

Leetcode Roman to Integer problem solution

In this Leetcode Roman to Integer problem solution Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.

Symbol       Value

I                   1

V                  5

X                 10

L                  50

C                 100

D                 500

M                1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written from largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  1. I can be placed before V (5) and X (10) to make 4 and 9. 
  2. X can be placed before L (50) and C (100) to make 40 and 90. 
  3. C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Leetcode Integer to Roman problem solution


Problem solution in Python.

class Solution:
    def romanToInt(self, s: str) -> int:
        maps = {'I' : 1,'V' : 5,'X' : 10,
        'L' : 50,'C' : 100,'D' : 500,'M' : 1000}
        sums = 0
        i = 0
        while i < len(s)-1:
            if maps[s[i]] < maps[s[i+1]]:
                sums += maps[s[i+1]]-maps[s[i]]
                i += 1
            else:
                sums += maps[s[i]]
            i += 1
        if i != len(s):
            sums += maps[s[-1]]
        return sums



Problem solution in Java.

import java.util.*;
class Solution {
public int romanToInt(String s) {
HashMap<Character,Integer>map=new HashMap<Character,Integer>();

    map.put('I',1);
     map.put('V',5);
     map.put('X',10);
     map.put('L',50);
     map.put('C',100);
     map.put('D',500);
     map.put('M',1000);
    int result=map.get(s.charAt(s.length()-1));
    for(int i=s.length()-1;i>0;i--)
    {
        if(map.get(s.charAt(i))<=map.get(s.charAt(i-1)))
        {
            result+=map.get(s.charAt(i-1));
        }
        else
        {
            result-=map.get(s.charAt(i-1));
        }
    }
    return result;
}
}


Problem solution in C++.

class Solution {
int getTranslateNum(char s) {
    switch(s) {
        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;
    }
    return 0;
}
public:
    int romanToInt(string s) {
        if(s.empty()) return 0;
        int returnValue = 0;
        for(unsigned int i=0; i<s.size()-1; i++) {
            if(getTranslateNum(s[i]) < getTranslateNum(s[i+1])) {
                returnValue -= getTranslateNum(s[i]);
            } else {
                returnValue += getTranslateNum(s[i]);
            }
        }
        returnValue += getTranslateNum(s[s.size()-1]);
        return returnValue;
    }
};


Problem solution in C.

int getValue(const char * s){
    switch(*s) {
        case 'I': return (s[1] == 'V' || s[1] == 'X') ? -1 : 1;
        case 'X': return (s[1] == 'L' || s[1] == 'C') ? -10 : 10;
        case 'C': return (s[1] == 'D' || s[1] == 'M') ? -100 : 100;
        case 'V': return 5;
        case 'L': return 50;
        case 'D': return 500;
        case 'M': return 1000;
    }
    return 0;
}

int romanToInt(char * s){
    int result = 0; 
    
    for(;*s != '\0'; ++s) {
        result += getValue(s);
    }
    return result;
}


Post a Comment

0 Comments