forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteger-to-roman.cpp
More file actions
26 lines (25 loc) · 855 Bytes
/
integer-to-roman.cpp
File metadata and controls
26 lines (25 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Time: O(n)
// Space: O(1)
class Solution {
public:
/**
* @param n The integer
* @return Roman representation
*/
string intToRoman(int n) {
map<int, string, greater<int>> numeral_map = {{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 result;
while (n > 0) {
for (const auto& kvp : numeral_map){
while (n / kvp.first > 0) {
n -= kvp.first;
result.append(kvp.second);
}
}
}
return result;
}
};