My Java solution, use a stack and an option characters array in a not hard-coded way


  • 1
    W
    public String intToRoman(int num) {
        // put all digits in a stack
        // {9, 9, 9, 3}
        // assign letter options
        // {{'X', 'V', 'I'}, {'C', 'L', 'X'}, {'M', 'D', 'C'}, {' ', ' ', 'M'}};
        // for each digit
        // cases are:
        // if (10 - d == 1)
        // if (10 - d > 1 && d - 5 >= 0)
        // if (5 - d == 1)
        // if (d - 0 > 0)
        Stack<Integer> stack = new Stack<Integer>();
        while (num > 0) {
            stack.push(num % 10);
            num = num / 10;
        }
    
        char[][] letterOptionsArr = {{'X', 'V', 'I'}, {'C', 'L', 'X'}, {'M', 'D', 'C'}, {' ', ' ', 'M'}};
    
        StringBuilder romanBuilder = new StringBuilder();
        while (stack.size() > 0) {
            int stackSize = stack.size();
            char[] letterOptions = letterOptionsArr[stackSize - 1];
            int digit = stack.pop();
            if (10 - digit == 1) {
                romanBuilder.append(letterOptions[2]);
                romanBuilder.append(letterOptions[0]);
            } else if  (10 - digit > 1 && digit - 5 >= 0) {
                romanBuilder.append(letterOptions[1]);
                for (int i = 0; i < digit - 5; i++) {
                    romanBuilder.append(letterOptions[2]);
                }
            } else if (5 - digit == 1) {
                romanBuilder.append(letterOptions[2]);
                romanBuilder.append(letterOptions[1]);
            } else {
                for (int i = 0; i < digit; i++) {
                    romanBuilder.append(letterOptions[2]);
                }
            }
        }
    
        return romanBuilder.toString();
    }

  • 0
    K

    Similar technique, arguably easier to read

    public class Solution {
    public String intToRoman(int num) {
        String[] units = {"I", "V", "X"};
        String[] tens = {"X", "L", "C"};
        String[] hundreds = {"C", "D", "M"};
        String[] thousands = {"M"};
        String[][] lookup = {units, tens, hundreds, thousands};
    
        int idx = 0;
        StringBuffer res = new StringBuffer();
        while (num > 0) {
            res.insert(0, generateRoman(num%10, lookup[idx]));
            idx++;
            num = num/10;
        }
        
        return res.toString();
    }
    
    public String generateRoman(int digit, String[] lookup) {
        StringBuffer res = new StringBuffer();
        switch (digit) {
            case 1:
            case 2:
            case 3:
                for (int i=0; i<digit; i++) {
                    res.append(lookup[0]);
                }
                break;
            case 4:
                res.append(lookup[0]);
                res.append(lookup[1]);
                break;
            case 5:
                res.append(lookup[1]);
                break;
            case 6:
            case 7:
            case 8:
                res.append(lookup[1]);
                for (int i=0; i<(digit-5); i++) {
                    res.append(lookup[0]);
                }
                break;
            case 9:
                res.append(lookup[0]);
                res.append(lookup[2]);
                break;
        }
        
        return res.toString();
    }
    

    }


Log in to reply
 

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