美文网首页
22. Integer to Roman FROM Leetco

22. Integer to Roman FROM Leetco

作者: 时光杂货店 | 来源:发表于2017-03-16 17:09 被阅读5次

题目

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

频度: 4

解题之法

class Solution {
public:
    string intToRoman(int num) {
        
    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/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
    }
};

相关文章

网友评论

      本文标题:22. Integer to Roman FROM Leetco

      本文链接:https://www.haomeiwen.com/subject/rqpjnttx.html