-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger_to_Roman.cpp
More file actions
67 lines (64 loc) · 2.11 KB
/
Integer_to_Roman.cpp
File metadata and controls
67 lines (64 loc) · 2.11 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
O(N) bits
O(N)
*/
class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
char a[3] = { 'V', 'L', 'D' };
char b[4] = { 'I', 'X', 'C', 'M' };
vector<char> strNum;
while (num) {
strNum.push_back(num % 10 + '0');
num = num / 10;
}
string result = "";
for (int i = strNum.size() - 1; i >= 0; i--) {
if (strNum[i] == '0') {
continue;
} else if (strNum[i] == '5') {
result.append(1, a[i]);
} else if (strNum[i] < '4') {
result.append(strNum[i] - '0', b[i]);
} else if (strNum[i] == '4') {
result.append(1, b[i]);
result.append(1, a[i]);
} else if (strNum[i] > '5' && strNum[i] < '9') {
result.append(1, a[i]);
result.append(strNum[i] - '5', b[i]);
} else {
result.append(1, b[i]);
result.append(1, b[i + 1]);
}
}
return result;
}
map<int, string> dict;
string intToRoman(int num) {
dict.clear();
dict[1] = "I";
dict[4] = "IV";
dict[5] = "V";
dict[9] = "IX";
dict[10] = "X";
dict[40] = "XL";
dict[50] = "L";
dict[90] = "XC";
dict[100] = "C";
dict[400] = "CD";
dict[500] = "D";
dict[900] = "CM";
dict[1000] = "M";
string result = "";
for (map<int, string>::reverse_iterator it = dict.rbegin();
it != dict.rend(); it++) {
while (num >= it->first) {
result += it->second;
num -= it->first;
}
}
return result;
}
};