2D String array: java code.


  • 0
    C
    public class Solution {
        public static final String[][] num0 = new String[][]{
                new String[]{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
                new String[]{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
                new String[]{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
                new String[]{"", "M", "MM", "MMM"}
        };
        public String intToRoman(int num) {
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while (num > 0) {
                sb.insert(0, num0[i++][num % 10]);
                num /= 10;
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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