-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.整数转罗马数字.cpp
54 lines (52 loc) · 1.24 KB
/
12.整数转罗马数字.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode.cn id=12 lang=cpp
*
* [12] 整数转罗马数字
*/
class Solution {
public:
string intToRoman(int num) {
std::unordered_map<int, std::string> m = {
{1, "I"},
{2, "II"},
{3, "III"},
{4, "IV"},
{5, "V"},
{6, "VI"},
{7, "VII"},
{8, "VIII"},
{9, "IX"},
};
std::unordered_map<int, char> mi = {
{0, 'I'},
{1, 'X'},
{2, 'C'},
{3, 'M'}
};
std::unordered_map<int, char> mv = {
{0, 'V'},
{1, 'L'},
{2, 'D'}
};
int cnt = 0;
std::vector<std::string> v(4, "");
while(num > 0){
int t = num % 10;
num /= 10;
v[cnt] = m[t];
std::transform(v[cnt].begin(), v[cnt].end(), v[cnt].begin(), [&](char c){
switch (c)
{
case 'X':
return mi[cnt+1];
case 'I':
return mi[cnt];
default:
return mv[cnt];
}
});
cnt++;
}
return v[3]+v[2]+v[1]+v[0];
}
};