LC12. Integer to Roman

https://leetcode.com/problems/integer-to-roman/

  • 또다른 구현 문제, 리스트업 하는데 시간을 다보낸듯…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    string intToRoman(int num) {
        vector<pair<int,string>> conv = {
            {1,   "I"},{2,   "II"},{3,   "III"},{4,   "IV"},{5,  "V"},{6,   "VI"},{7,  "VII"},{8,  "VIII"},{9,  "IX"},
            {10,  "X"},{20,  "XX"},{30,  "XXX"},{40,  "XL"},{50, "L"},{60,  "LX"},{70, "LXX"},{80, "LXXX"},{90, "XC"},
            {100, "C"},{200, "CC"},{300, "CCC"},{400, "CD"},{500,"D"},{600, "DC"},{700,"DCC"},{800,"DCCC"},{900,"CM"},
            {1000,"M"},{2000,"MM"},{3000,"MMM"},
        };
        reverse(conv.begin(), conv.end());
        string ans;
        for(int i = 0; i < conv.size(); i++) {
            const auto [arab, rome] = conv[i];
            if(arab <= num) {
                num -= arab;
                ans += rome;
            }
        }
        return ans;
    }
};