C++ concise solution.


  • 0
    C
    string intToRoman(int num) {
        unordered_map<int, string> myMap = {{1,"I"}, {4,"IV"}, {5,"V"}, {9,"IX"}, {10,"X"}, 
        {40,"XL"}, {50,"L"}, {90,"XC"}, {100,"C"}, {400,"CD"}, {500,"D"}, {900,"CM"}, {1000,"M"}};
        string res = "";
        for (auto& m: {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}) { 
            if (num) {
                for (int i = 0; i < num/m; i++)
                    res += myMap[m];
                num %= m;
            }
        }
        return res;
    }

Log in to reply
 

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