LeetCode 12. Integer to Roman
Last updated
Last updated
Input: num = 4
Output: "IV"Input: num = 9
Output: "IX"Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.class Solution {
public:
string intToRoman(int num) {
map<int, string> mymap{
{1, "I"}, {5, "V"}, {10, "X"}, {50, "L"},
{100, "C"}, {500, "D"}, {1000, "M"},
{4, "IV"}, {9, "IX"},
{40, "XL"}, {90, "XC"},
{400, "CD"}, {900, "CM"}
};
stringstream ss;
for (auto it = mymap.rbegin(); it != mymap.rend(); it++) {
int key = it->first;
const string& code = it->second;
while (num >= key) {
num -= key;
ss << code;
}
}
return ss.str();
}
};