-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegerToRoman.cpp
More file actions
36 lines (36 loc) · 856 Bytes
/
integerToRoman.cpp
File metadata and controls
36 lines (36 loc) · 856 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
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
string intToRoman(int num) {
string temp;
unordered_map<int,string> map;
map[1] = "I";
map[4] = "IV";
map[5] = "V";
map[9] = "IX";
map[10] = "X";
map[40] = "XL";
map[50] = "L";
map[90] = "XC";
map[100] = "C";
map[400] ="CD";
map[500] = "D";
map[900] = "CM";
map[1000] = "M";
vector<int> key;
for(auto s : map)
{
key.push_back(s.first);
}
sort(key.begin(),key.end(),[](int a,int b){return a>b;});
for(int i =0;i<key.size();i++)
{
int times = num/key[i];
for(int j = 0;j<times;j++)
{
temp+=map[key[i]];
}
num-=(times*key[i]);
}
return temp;
}
};