-
Notifications
You must be signed in to change notification settings - Fork 1
/
12.cpp
29 lines (22 loc) · 870 Bytes
/
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
// 12. Integer to Roman - https://leetcode.com/problems/integer-to-roman
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
vector<string> M = {"", "M", "MM", "MMM"};
vector<string> C = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
vector<string> X = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
vector<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];
}
};
int main() {
ios::sync_with_stdio(false);
Solution solution;
assert(solution.intToRoman(621) == "DCXXI");
assert(solution.intToRoman(21) == "XXI");
assert(solution.intToRoman(9) == "IX");
assert(solution.intToRoman(3) == "III");
return 0;
}