Header Ad

Leetcode Integer to Roman problem solution

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


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)



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]='\0';    
    return ret;    
}


Post a Comment

0 Comments