Simple Java 9 ms solution (Beat 100%)


  • 0
    S
    public String intToRoman(int num) {
            int [] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
            String [] letters = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
            StringBuilder roman = new StringBuilder();
            int n = numbers.length;
            
            for(int i = 0; i < n; i++) {
                while(num >= numbers[i]) {
                    roman.append(letters[i]);
                    num = num - numbers[i];
                }
            }
            return roman.toString();
        }
    

Log in to reply
 

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