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 Integer to Roman problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Integer to Roman 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 an integer, convert it to a roman numeral.

Leetcode Integer to Roman problem solution

Leetcode Integer to Roman problem solution in Python.

class Solution:
    def intToRoman(self, num: int) -> str:
        sym = [(1,'I'),(4,'IV'),(5,'V'),(9,'IX'),
               (10,'X'),(40,'XL'),(50,'L'),(90,'XC'),
               (100, 'C'),(400,'CD'),(500, 'D'),(900, 'CM'),
               (1000, 'M')]
        
        i = len(sym)-1
        res = []
        while num:
            quo = num // sym[i][0]
            res.append(sym[i][1]*quo)
            num = num % sym[i][0]
            i -= 1
        return ''.join(res)

Integer to Roman problem solution in Java.

class Solution {
    
    public Map<Integer,String> getMapping(){
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"I");
        map.put(5,"V");
        map.put(10,"X");
        map.put(50,"L");
        map.put(100,"C");
        map.put(500,"D");
        map.put(1000,"M");
        map.put(4,"IV");
        map.put(9,"IX");
        map.put(40,"XL");
        map.put(90,"XC");
        map.put(400,"CD");
        map.put(900,"CM");
        return map;
    }
    
    public String intToRoman(int num) {
        Map<Integer,String> map = getMapping();
        StringBuilder sb = new StringBuilder();
        int[] order = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
        int i=0;
        while(num!=0){
            if(num/order[i]>0){
                sb.append(map.get(order[i]));
                num-=order[i];
            } else{
                i++;
            }
        }
        return sb.toString();
    }
}

Problem solution in C++.

class Solution {
public:
    string intToRoman(int num) {
        vector<string> roman({"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"});
        vector<int> int_arr({1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000});
        int i=12,div;
        string ans="";
        while(num>0){
            div = num/int_arr[i];
            num = num%int_arr[i];
            while(div--) ans+=roman[i];
            i--;
        }
        return ans;
    }
};

Problem solution in C.

char * intToRoman(int num){
    char *ret=malloc(30*sizeof(char));
    
    int cnt=0; int i=0;
    if(num/1000>0){
        for(i=0;i<num/1000;i++)
            ret[cnt++]='M';
        num=num%1000;
    }
    if(num/900>0){ 
        ret[cnt++]='C'; ret[cnt++]='M'; num=num%900;
    }else{
        if(num/500>0){
            ret[cnt++]='D'; num=num-500;            
        }else if(num/400>0){
            ret[cnt++]='C'; ret[cnt++]='D'; num=num-400;
        }
        if(num/100>0){
            for(i=0;i<num/100;i++)
                ret[cnt++]='C';            
            num=num%100;
        }        
    }    
    if(num/90>0){
        ret[cnt++]='X'; ret[cnt++]='C'; num=num%90;
    }else{
        if(num/50>0){
            ret[cnt++]='L'; num=num-50;            
        }else if(num/40>0){
            ret[cnt++]='X'; ret[cnt++]='L'; num=num-40;
        }
        if(num/10>0){
            for(i=0;i<num/10;i++)
                ret[cnt++]='X';
            num=num%10;
        }        
    }    
    if(num/9>0){
        ret[cnt++]='I'; ret[cnt++]='X';
    }else{
        if(num/5>0){
            ret[cnt++]='V'; num=num-5;            
        }else if(num/4>0){
            ret[cnt++]='I'; ret[cnt++]='V'; num=num-4;
        }
        if(num>0){
            for(i=0;i<num;i++)
                ret[cnt++]='I';
        }        
    }
    
    ret[cnt]='';    
    return ret;    
}

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