Leetcode 12. Integer to Roman

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Integer to Roman

Integer to Roman

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
string intToRoman(int num) {
const int TEN = 10;
const int HUNDRED = 100;
const int THOUSAND = 1000;
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/THOUSAND] + C[(num%THOUSAND)/HUNDRED]+ X[(num%HUNDRED)/TEN] + I[num%TEN];
}
};

Reference

  1. https://leetcode.com/problems/integer-to-roman/description/
如果有收获,可以请我喝杯咖啡!