My intToRoman


  • 0
    U

    1). We need to understand the roman number's logic.
    2). Define a array to store key value of roman, then convert number to 4 units.

        string intToRoman(int num) {
            string value[4][10] = {"", "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"};
            string roman;
            roman.append(value[3][num /1000 % 10]);
            roman.append(value[2][num /100 % 10]);
            roman.append(value[1][num /10 % 10]);
            roman.append(value[0][num % 10]);
            
            return roman;
        }
    

Log in to reply
 

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