Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Roman to Integer problem solution

YASH PAL, 31 July 202418 January 2026

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

Leetcode Roman to Integer 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

Roman to Integer 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 != ''; ++s) {
        result += getValue(s);
    }
    return result;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes