My short java code


  • 6
    A
    public class Solution {
        
        String[][] base = new String[][]{{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
                                    {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
                                    {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
                                    {"M", "MM", "MMM", "", "", "", "", "", ""}};
        
        public String intToRoman(int num) {
            String r = "";
            for (int i = 0; num != 0; num /= 10, i++) {
                if (num % 10 == 0)
                    continue;
                r = base[i][num % 10-1] + r;
            }
            return r;
        }
    }

Log in to reply
 

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