Solution in C++


  • 0
    C
    class Solution
    {
    public:
        string intToRoman(int num)
        {
            const string numerals[] =
            {
                "M", "CM", "D", "CD", "C",
                "XC", "L", "XL", "X", "IX",
                "V", "IV", "I"
            };
    
            const int numeralValues[] =
            {
                1000, 900, 500, 400, 100,
                90, 50, 40, 10, 9, 5, 4, 1
            };
            
            string roman = "";
            
            for(int i = 0; i < 13; i++)
            {
                while(num >= numeralValues[i])
                {
                    roman += numerals[i];
                    
                    num -= numeralValues[i];
                }
            }
            
            return roman;
        }
    };
    

Log in to reply
 

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