Sharing my java solution


  • 0
    F
    public String intToRoman(int num) {
            int[] radix = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
            String[] symbol = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
            StringBuffer stringBuffer = new StringBuffer("");
            for (int i = 0; num > 0; i++) {
                int count = num / radix[i];
                num %= radix[i];
                while (count-- > 0) {
                    stringBuffer.append(symbol[i]);
                }
            }
            return stringBuffer.toString();
        }

Log in to reply
 

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