forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteger to Roman.cpp
60 lines (60 loc) · 1.2 KB
/
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
// Runtime: 3 ms (Top 96.82%) | Memory: 6 MB (Top 78.88%)
class Solution {
public:
string intToRoman(int num) {
string ans="";
while(num>=1000){
ans += 'M';
num -= 1000;
}
while(num>=900){
ans += "CM";
num -= 900;
}
while(num>=500){
ans += 'D';
num -= 500;
}
while(num>=400){
ans += "CD";
num -= 400;
}
while(num>=100){
ans += 'C';
num -= 100;
}
while(num>=90){
ans += "XC";
num -= 90;
}
while(num>=50){
ans += 'L';
num -= 50;
}
while(num>=40){
ans += "XL";
num -= 40;
}
while(num>=10){
ans += 'X';
num -= 10;
}
while(num>=9){
ans += "IX";
num -= 9;
}
while(num>=5){
ans += 'V';
num -= 5;
}
while(num>=4){
ans += "IV";
num -= 4;
}
while(num>0){
ans += 'I';
num -= 1;
}
return ans;
}
};