forked from fengvyi/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
012. Integer to Roman.cpp
76 lines (75 loc) · 1.97 KB
/
012. Integer to Roman.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Top-voted solution (https://discuss.leetcode.com/topic/12384/simple-solution).
class Solution {
public:
string intToRoman(int num) {
string M[] = {"", "M", "MM", "MMM"};
string C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
};
// While my solution is...
class Solution {
public:
string intToRoman(int num) {
//'I' = 1, 'X' = 10, 'C' = 100, 'M' = 1000, 'V' = 5, 'L' = 50, 'D' = 500;
// Subtractive Notation
// Number 4 9 40 90 400 900
// Notation IV IX XL XC CD CM
string res = "";
while(num >= 1000){
num -= 1000;
res.push_back('M');
}
if(num >= 900){
num -= 900;
res.append("CM");
}
if(num >= 500){
num -= 500;
res.push_back('D');
}
if(num >= 400){
num -= 400;
res.append("CD");
}
while(num >= 100){
num -= 100;
res.push_back('C');
}
if(num >= 90){
num -= 90;
res.append("XC");
}
if(num >= 50){
num -= 50;
res.push_back('L');
}
if(num >= 40){
num -= 40;
res.append("XL");
}
while(num >= 10){
num -= 10;
res.push_back('X');
}
if(num >= 9){
num -= 9;
res.append("IX");
}
if(num >= 5){
num -= 5;
res.push_back('V');
}
if(num >= 4){
num -= 4;
res.append("IV");
}
while(num > 0){
num -= 1;
res.push_back('I');
}
return res;
}
};