Skip to content

Commit a8035ff

Browse files
Create integer_to_roman.cpp
1 parent eaff4a5 commit a8035ff

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

integer_to_roman.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string intToRoman(int num) {
4+
if(num == 0) return "";
5+
6+
string table[4][10] = {
7+
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
8+
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
9+
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
10+
{"", "M", "MM", "MMM"}
11+
};
12+
13+
//2538 => run through this example when revisiting code.
14+
15+
int count = 0; //gives the index of row in table.. 10's place, 100's place, 1000's place
16+
string result = "";
17+
while(num > 0) {
18+
int value = num % 10; //gives the index in the row of table basicall
19+
result = table[count][value] + result;
20+
count++;
21+
num /= 10;
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)