Short 32ms C++ solution


  • 0
    D
    class Solution {
    public:
        string intToRoman(int num) {
            typedef pair<string, int> Entry;
            vector<Entry>  mp { {"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100}, {"XC", 90}, {"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1} };
            string result;
            while (num) {
                auto i = find_if(mp.begin(), mp.end(), [num](Entry p) {return num >= p.second;});
                result += i->first, num -= i->second;
           } 
           return result; 
        }
    };

Log in to reply
 

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