Share my C++ brief solution 30ms


  • 1
    class Solution {
    public:
        string intToRoman(int num) {
            string roman[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 result;
            int cur, R = 0;
            while(num)
            {
                cur = num % 10;
                string tmp(roman[R++][cur]);
                result = tmp.append(result);
                num /= 10;
            }
            return result;
        }
    };

  • 0
    M

    great solution, easy to understand.:)


Log in to reply
 

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