LeetCode:Integer to Roman

Posted walker lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Integer to Roman相关的知识,希望对你有一定的参考价值。

Integer to Roman




Total Accepted: 64703 Total Submissions: 165064 Difficulty: Medium

Given an integer, convert it to a roman numeral.

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

Subscribe to see which companies asked this question

Hide Tags
 Math String














c++ code:

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];
    }
};


以上是关于LeetCode:Integer to Roman的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]Integer to Roman

Leetcode:Integer To Roman(C语言版)

Leetcode:Integer To Roman(C语言版)

Leetcode:integer_to_roman

Integer to Roman/Roman to Integer

13. Roman to Integer