Simple Java Solution!!!


  • 1
    A
    public class Solution {
        public String intToRoman(int num) {
            int multiplier = 1;
            String output = "";
            while(num != 0){
                output = calcRoman(num%10 * multiplier,multiplier) + output;
                multiplier *= 10;
                num = num/10;
            }
            return output;
        }
        
        public String calcRoman (int num, int multiplier){
            String op = "";
            if(num == 0){
                return op;
            }
            if (multiplier == 1){
                num = num / multiplier;
                if (num < 4){
                    while (num != 0){
                        op += "I"; 
                        num -= 1; 
                    }
                }
                else {
                    if (num == 4){
                        op = "IV";
                    }
                    else if(num == 9){
                        op = "IX";
                    }
                    else {
                        op += "V";
                        while (num != 5){
                            op += "I"; 
                            num -= 1; 
                        }
                    }
                }
            }
            else if (multiplier == 10){
                num = num / multiplier;
                if (num < 4){
                    while (num != 0){
                        op += "X"; 
                        num -= 1; 
                    }
                }
                else {
                    if (num == 4){
                        op = "XL";
                    }
                    else if(num == 9){
                        op = "XC";
                    }
                    else {
                        op += "L";
                        while (num != 5){
                            op += "X"; 
                            num -= 1; 
                        }
                    }
                }
            }
            else if (multiplier == 100){
                num = num / multiplier;
                if (num < 4){
                    while (num != 0){
                        op += "C"; 
                        num -= 1; 
                    }
                }
                else {
                    if (num == 4){
                        op = "CD";
                    }
                    else if(num == 9){
                        op = "CM";
                    }
                    else {
                        op += "D";
                        while (num != 5){
                            op += "C"; 
                            num -= 1; 
                        }
                    }
                }
            }
            else if (multiplier == 1000){
                num = num / multiplier;
                if (num < 4){
                    while (num != 0){
                        op += "M"; 
                        num -= 1; 
                    }
                }
            }
            return op;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.